How I can attach multiple image to facebook feed message? - facebook

Right now I use this method to add feed message:
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => $this->cafe->getUrlForPost(),
'name' => $this->cafe->getTextForPost(),
'picture' => 'http://' . $_SERVER['HTTP_HOST'] . $this->getImageForPost()
)
))->execute()->getGraphObject();
Is there any way to add a multiple image in picture field? Or maybe do you have a workaround for this?
Thank you for your answers.

Unfortunately, right now you can´t add more than one image in one API call. You can only do multiple calls and you can use Batch Requests for it: https://developers.facebook.com/docs/graph-api/making-multiple-requests

Related

Facebook PHP SDK: Post attached photo to fanpage

in my working code to post a message with url-based image, i have this setup:
$post_data = array(
'message' => 'nice kitten pic',
'url' => 'http://www.eastcottvets.co.uk/uploads/Animals/gingerkitten.jpg'
);
$request = new FacebookRequest($app, $access_token, 'POST', '/' . $page_id . '/photos', $post_data);
i tryed this, which does not work:
$post_data = array(
'message' => 'nice kitten pic',
'source' => realpath('pics/kitten.jpg')
);
But now i like to post an image, not with an url reference, but with a reference to the path of the image on my localhost.
Facebook Docu says:
"There are two separate ways of publishing photos to Facebook:
1: Attach the photo as multipart/form-data. The name of the object doesn't matter, but historically people have used source as the parameter name for the photo. How this works depends on the SDK you happen to be using to do the post.
2: Use a photo that is already on the internet by publishing using the url parameter:"
So, way Nr.2 works, but how do i use way nr.1, with "attach the photo as multipart/form-data" ?? - the docu laks an example for php sdk and in general.
the docu laks an example for php sdk and in general.
No, it doesn’t – you’ve just missed it: https://developers.facebook.com/docs/php/howto/example_upload_photo
The part most relevant to your question is the use of the fileToUpload to prepare a local file for upload.
Solution, for people wo might search aswell:
$post_data = array(
'message' => 'nice kitten pic',
'source' => $fb->fileToUpload('/var/www/myPublicToTheWebFolder/kitten.jpg'),
);
of course the path will be different, depending on server setup. but it has to be the full path you have to output there.

Facebook API photo upload from URL not working

Trying to upload photos by access token, album_id or user_id and URL but getting an error:
"(#324) Requires upload file"
"OAuthException"
I've checked the token and it's valid. Also upload from file works, but upload from URL does not. Facebook does not seem to want to recognise the "url" parameter. This is strange because I swear the other day when I tested this it worked fine.
Is anyone else having this problem?
My code:
$graph_url = "https://graph.facebook.com/ALBUM_ID/photos?access_token=TOKEN";
$postdata = http_build_query( array('message' => 'hello', 'url' => IMAGE_URL);
$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));
I've also checked, it works in FB's graph API explorer, but does not work when using any REST client tool either.
UPDATE:
I got it to work by switching the order of the array inside http_build_query so that the URL is first (or removing the 'message' parameter). The second element is always ingored.
Can anyone see what I'm doing wrong? Why is the second parameter ignored in this case? While the upload "works" I'd still like the 'message' parameter to work so that the image description can also be loaded.
When you’re uploading an image via URL, you must not use the Content-type application/x-www-form-urlencoded – that’s for real file uploads done from your server, using the source parameter, only.

Facebook create an album graph api

I have written a facebook app which checks if an album exists, if it does not it creates one.
This was all working 2 weeks ago and now I go and check it to show the client and it is no longer working. I was thinking it could have something to do with Facebook and maybe they now require an SSL? I can't find any evidence of this on there website however.
The error I am getting is:
Warning: file_get_contents(https://graph.facebook.com/517578134925015/albums?access_token=AAAD5tUCp808BAIZBPk4eV11mlf9C92velLsDeZAm5mXhKZCkwpM3LNy7ax6BBmhuH4BVZBUN6Iycyt55NoXZAFSts9zHeCFNzT6FLJYucgT2SQG8fOYIP) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in /var/www/vhosts/hidden/development/cms/backend/publishAlbum.php on line 59
My current code is:
//If no album ID is set, create a new album.
$graph_url = "https://graph.facebook.com/".$pageId."/albums?" . "access_token=". $pageAccessToken;
$postdata = http_build_query(
array(
'name' => $albumName
)
);
$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));
Any help is appreciated.
Thanks
EDIT:
The code I tried that uses the API.
$params = array(
'name' => 'testtest',
'message' => 'test test'
);
try{
$albumtResult = $facebook->api('/'.$pageId.'/albums', 'post', $params);
}
catch(FacebookApiException $e){
error_log($e);
$albumtResult = null;
}
$albumId = $albumtResult->id;
Here is the caught exception: http://pastebin.com/pxeyRTDn
Well, the reason for this error would be much easier to figure out, if you’d used the PHP SDK to make API calls, instead of doing them “by hand“ using file_get_contents … because using the SDK, you get an exception that should point to the error cause, whereas file_get_contents (by default) just throws a warning when it gets anything else than a 2xx HTTP status code for an answer, and does not let you access the response body (which will most likely contain a human-readable error message as well).
My advice: Use the SDK!
Otherwise, add to the context parameter that you want to see the returned data even if the status code signals an error. (details -> see manual)
After a lot of searching, someone created a bug report on Facebook that was almost identical to my problem.
Since being created it has had a lot of repo's and other people reporting that they are also having the same issue.
The priority on facebook has been changed to high so hopefully it gets fixed soon.
Facebook bug report: https://developers.facebook.com/bugs/376442565760536?browse=search_50361119879b15494037772
Thanks for everyones time.

Is it possible to do batch POST API calls?

I'm writing an app that makes a daily post as a user, and having benchmarked the PHP code that does this, it seems to take about two seconds per user. I'm dividing the work up in to chunks, and using multiple cron jobs to do each chunk. I'd like to scale to many thousands of users one day, but this kind of work load is just too much. It would take my server literally all day to post to each user one at a time using this method.
How do people normally do this? I've seen other apps that do it. Is there some way of sending all these posts off at once using just one API call? Using individual API calls per user is crazy slow.
Thanks.
On one hand, this is entirely dependent on the API.
However, you could use a multi-threaded or pseudo-parallel approach to this, such that your program sends, say, 100 HTTP POST requests at a time, rather than generating one request after another in series.
Since you're using PHP, multi-threading is out (I think) but this question is very similar to others. For example, see how these folks recommend curl_multi.
You can use batch query to achieve what you need.
The code for batch query is mentioned below. You can refer more about Facebook batch query at : http://25labs.com/tutorial-post-to-multiple-facebook-wall-or-timeline-in-one-go-using-graph-api-batch-request/
$body = array(
'message' => $_POST['message'],
'link' => $_POST['link'],
'picture' => $_POST['picture'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description'],
);
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID1}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID2}/feed",
'body' => http_build_query($body) );
$batchPost[] = array(
'method' => 'POST',
'relative_url' => "/{ID3}/feed",
'body' => http_build_query($body) );
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');

query Canvas URL using FQL

It seems like http://developers.facebook.com/docs/reference/fql/application/ provides all the information except for Canvas URL. Is it possible to query this information any other way?
Well, the solution I came up with, was simply calling the old admin.getAppProperties method.
$info = $facebook->api(array(
'method' => 'admin.getAppProperties',
'properties' => array('publish_url'),
));