Get list of Page Albums using Facebook PHP SDK v5

我的未来我决定 提交于 2019-12-09 00:01:20

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/32395951/get-list-of-page-albums-using-facebook-php-sdk-v5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!