I have a Facebook App that POST a picture to a specific album created also by my app. If I run the app the second time it will create another album and POST the new picture there. I would like if the user POST a new one, the picture to be placed in the same album and if is not there to be created. Here is my code so far:
$graph_url = "https://graph.facebook.com/me/albums?" . "access_token=". $access_token;
$album_name = 'MyApp Album';
$album_description = 'Album description';
$postdata = http_build_query(
array('name'=> $album_name,
'message'=> $album_description
));
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=> 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$album_id = $result->id;
$file = $_POST['photo'];
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = "https://graph.facebook.com/".$album_id."/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
print_r(json_decode($data,true));
I don't wanna use FQL queries if isn't absolutely neccesary. If I can search probably for the album name and then return the ID it would be great.
Related
I have tried this to upload a file from a folder in my desktop to a folder in dropbox account.
but each time i have uploaded an empty file through this code.
How it is possible?
Below s my code:
$ch = curl_init();
$TOKEN = "asasasa";//token here
$url = 'https://content.dropboxapi.com/2/files/upload';
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: multipart/form-data';
$headers[] = 'Dropbox-API-Arg:{"path":"/home/new/ofd","mode":{".tag":"add"},"autorename":false,"mute":false}';
$headers[] = "Authorization: Bearer ".$TOKEN;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$droplist = json_decode($response,true);
You don't seem to be adding the file content to the upload call anywhere, so an empty file is expected from your code.
You can find an example of using /2/files/upload with curl in PHP below. That uses CURLOPT_INFILE to add the file content itself.
<?php
$path = 'test_php_upload.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);
$cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
fclose($fp);
?>
<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.
Upload a file into dropbox folder dropbox api v2
$dropbox_api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
$token = 'Access token value put here'; // should be replaced with the OAuth 2 access token
$headers = array('Authorization: Bearer '. $token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)
)
);
$ch = curl_init($dropbox_api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = $filename;
$fp = fopen($path, 'rb');
$filesize = filesize($path);
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//For display value as array object starts here
echo "<pre>";
print_r(json_decode($response));
//For display value as array object ends here
echo($response.'<br/>');
echo($http_code.'<br/>');
curl_close($ch);
I am trying to upload video to facebook page as page admin with the call to action:
// Post Data
$action_call_array = array('type' => 'LEARN_MORE', 'value' => array('link' => 'http://www.google.com'));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'call_to_action' => $action_call_array,
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos';
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;
var_dump($return);
Without the call to action, it works. But after adding the call to action, it returns me the following error:
string(196) "{"error":{"type":"Exception","message":"No Call To Action Type was parseable. Please refer to the call to action api documentation","code":1373054,"is_transient":false,"fbtrace_id":"H9GksxYBdQx"}}"
Looks like the way I add the call to action is not correct. But I don't really know how to fix it.
I put the call to action into url parameter and it is working now!
// Post Data
$fb_action = 'LEARN_MORE';
$fb_action_link = 'http://www.google.com';
$action_call_array = array('type' => $fb_action, 'value' => array('link' => $fb_action_link));
$params = http_build_query(array('call_to_action' => $action_call_array));
$data = [
'title' => 'Demo Title',
'description' => 'Demo Description
http://www.google.com
http://hk.yahoo.com',
'source' => new CURLFile('video/video_1mb.mp4', 'video/mp4'),
'access_token' => FACEBOOK_PAGE_TOKEN, // MUST BE INCLUDED !!!
];
// Post Url
$post_url = 'https://graph-video.facebook.com/'.FACEBOOK_PAGE_ID.'/videos?'.urldecode($params);
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
// result
$j = json_decode($return);
$page_post_id = $j->id;
This is my code and I tried to "Curl" it (file_get_contents didn't work for some reason) but since i can't really get Curl to work, I came here to get some help.
I've been suffering with this for 10 hrs now and I still can't find out!!! please help!!
<?php
$app_id = "xxxxx";
$app_secret = "xxxxx";
$fanpage_id ='3xxxxx';
$post_login_url = "xxxxxxxxxteszt.php";
$photo_url = "xxxxxxxxxx20130412104817.jpg";
$photo_caption = "sasdasd";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if (!$code)
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='".$dialog_url."'</script>");
}
if(isset($_REQUEST['code'] ))
{
print('<script>alert("asd");</script>');
function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $url );
}
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
print($code);
$response = curl($token_url);
print($response);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/".$fanpage_id."/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo curl($graph_url);
echo '</body></html>';
}
?>
You can't just pass the url to the CURLOPT_POSTFIELDS option. The base url should be set in the CURLOPT_URL option, and the parameters in CURLOPT_POSTFIELDS (using PHP's http_build_query function to generate the encoded query string);
So your curl function could look something like this:
function curl($url,$parms)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms, null, '&'));
}
And then you would call it like this:
$token_url="https://graph.facebook.com/oauth/access_token";
$token_parms = array(
"client_id" => $app_id,
"client_secret" => $app_secret,
"redirect_uri" => $post_login_url,
"code" => $code
);
$response = curl($token_url, $token_parms);
If I use the Facebook API to publish a photo to a page they appear as single story.
$args = array(
'message' => '',
);
$args["picture"] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
If I publish from Facebook they appear correctly as a single story for each picture.
How to do this programmatically?
I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Any ideas??
Thanks
The API url starts with https:// not http://. That could be the issue.
I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.