Skip to content

Instantly share code, notes, and snippets.

@mmohiudd
Created May 8, 2014 22:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mmohiudd/d85d9ccdf6c7b165c357 to your computer and use it in GitHub Desktop.
Save mmohiudd/d85d9ccdf6c7b165c357 to your computer and use it in GitHub Desktop.
Batch call with facebook-php-sdk-v4
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
// Use one of the helper classes to get a FacebookSession object.
// FacebookRedirectLoginHelper
// FacebookCanvasLoginHelper
// FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('access-token-here');
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
$params = [
[ // call 0 - get current logged in user
"method" => "GET",
"relative_url" => "me"
],
[ // call 1 - get current logged in user's friends
"method" => "GET",
"relative_url" => "me/friends"
],
[ // call 3 - get current logged in user's likes
"method" => "GET",
"relative_url" => "me/likes"
],
[// call 4 - get current logged in user's albums and photos
"method" => "GET",
"relative_url" => "method/fql.multiquery/?queries=" . json_encode([
"albums" => urlencode("SELECT aid, object_id, type, name, visible, owner, cover_pid, cover_object_id, visible, photo_count, video_count FROM album WHERE owner=me()"),
"album_covers" => urlencode("SELECT src_big, src_small, images, aid FROM photo WHERE pid IN (SELECT cover_pid FROM #albums)"),
"photos" => urlencode("SELECT pid, object_id, owner, src_big, src_small, images, aid FROM photo WHERE aid IN (SELECT aid FROM #albums)")
])
];
$response = (new FacebookRequest($session, 'POST', '?batch='.json_encode($params) ))->execute();
$objects = $response->getGraphObject();
foreach($objects->asArray() as $object){
$body = json_decode($object->body, 1);
print_r($body);
echo "--------------\n";
}
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
@rajeshvaya
Copy link

Thanks, just what i was looking for! :)

@cadupont
Copy link

Why do you execute a request at line 18?

@stephandesouza
Copy link

I guess something has changed on Facebook SDK, the batch is no more a path, and is now accepted only via parameter, see my fork: https://gist.github.com/stephandesouza/19a40a9105fb2bb6a90e#file-facebook-batchcall-php-L46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment