Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of Page Albums using Facebook PHP SDK v5

I want to use Facebook PHP SDK v5 to get a list of photo albums for a Page.

I am following the instructions at https://developers.facebook.com/docs/graph-api/reference/v2.4/page/albums, which are as follows:

/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
  $session,
  'GET',
  '/{page-id}/albums'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */

This does however seem to be incorrect as there is no "execute" function in the FacebookRequest class.

In addition, there also needs to be the access_token passed as the second variable to the FacebookRequest construct.

My full code is as follows:

require_once("classes/Facebook/autoload.php");
use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
$config = array();
$config['app_id'] = '{app_id}';
$config['app_secret'] = '{secret}';
$config['default_graph_version'] = 'v2.4';
$fb = new Facebook($config);
$app= new FacebookApp($config['app_id'],$config['app_secret']);

$request=new FacebookRequest($app,"{access_token}",'GET','/{page_id}/albums');
$response = $request->execute();
$graphObject = $response->getGraphObject();
print_r($graphObject);

This just produces the

fatal error: Call to undefined method Facebook\FacebookRequest::execute()

Can someone point me in the direction of the correct code?

like image 929
Pandy Legend Avatar asked Sep 04 '15 10:09

Pandy Legend


1 Answers

Have a look at the docs at

  • https://developers.facebook.com/docs/php/FacebookRequest/5.0.0#overview
  • https://developers.facebook.com/docs/php/gettingstarted/5.0.0#making-requests

Sample code:

$fbApp = new Facebook\FacebookApp('{app-id}', '{app-secret}');
$request = new Facebook\FacebookRequest($fbApp, '{access-token}', 'GET', '/{page_id}/albums');

// OR

$fb = new Facebook\Facebook(/* . . . */);
$request = $fb->request('GET', '/{page_id}/albums');

// Send the request to Graph
try {
  $response = $fb->getClient()->sendRequest($request);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$graphNode = $response->getGraphNode();

print_r($graphNode);
like image 149
Tobi Avatar answered Oct 17 '22 10:10

Tobi