php - 使用 Facebook PHP SDK v5 获取页面相册列表

标签 php facebook-graph-api facebook-php-sdk

我想使用 Facebook PHP SDK v5 获取主页的相册列表。

我正在按照 https://developers.facebook.com/docs/graph-api/reference/v2.4/page/albums 中的说明进行操作,具体如下:

/* 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 */

但这似乎是不正确的,因为在 FacebookRequest 类中没有“execute”函数。

此外,还需要将 access_token 作为第二个变量传递给 FacebookRequest 构造。

我的完整代码如下:

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

这只会产生

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

有人可以指出正确代码的方向吗?

最佳答案

查看文档

示例代码:

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

关于php - 使用 Facebook PHP SDK v5 获取页面相册列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32395951/

相关文章:

ruby-on-rails - 从 Rails 发布到 Facebook 粉丝专页墙

facebook - 有没有办法使用 Marketing API 更新 FB 广告 URL 参数?

android - 在没有授权 token 的情况下获取 Facebook 页面的公开帖子

php - 从 mysql 加载字符串作为 xml

php - Curl:保存文件而不是打开文件

Web 服务器上的 PHP Websocket

php - WordPress 选择计数查询优化

php - 使用 Graph API 更改旧 Facebook 帖子的隐私

facebook - 链接到 Facebook 时间轴 View 上的年/月页面

php - 适用于 PHP 5.3 的 Facebook SDK,可与 Graph API v2.x 一起使用