Facebook SDK 4 for PHP - 无需登录抓取一些事件

Posted

技术标签:

【中文标题】Facebook SDK 4 for PHP - 无需登录抓取一些事件【英文标题】:Facebook SDK 4 for PHP - no login grab some events 【发布时间】:2014-05-31 01:16:55 【问题描述】:

我尝试使用 Facebook SDK 4 for php

但我不明白一件事:将其与应用访问令牌一起使用!

我尝试了不同的令牌来初始化 FaceBookSession,但我不明白它是如何工作的...:/

以这个示例代码为例:

$request = new FacebookRequest(
  $session,
  '/480602392011337/events?fields=id,end_time,description,is_date_only,location,name,start_time&since='.date('Y-m-d',strtotime('-1 year')).'&until='.date('Y-m-d',strtotime('+1 year')).'&limit=500',
  //'/480602392011337/events',
  'GET'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

感谢您的帮助! :)

【问题讨论】:

我不知道如何获得有效的访问令牌...使用此命令:$session = new FacebookSession( 'xxxxxxxxx' ); 【参考方案1】:

我终于找到了解决方案(感谢 Google),只需使用FacebookSession::newAppSession();

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xxxxxx','xxxxxxx');
//  Get the access token for this app
$session = FacebookSession::newAppSession();

// make the API call, $datas contains the events for the facebook fan page BDE Polytech Marseille
try 
$request = new FacebookRequest(
    $session,
    'GET',
    '/page-id/events?fields=id,end_time,description,is_date_only,location,name,start_time&since='.strtotime('-1 year').'&until='.strtotime('+1 year').'&limit=500'
);
$response = $request->execute();

$datas = $response->getGraphObject()->asArray();

catch (FacebookRequestException $e) 
    echo $e->getMessage();
catch (\Exception $e) 
    echo $e->getMessage();

【讨论】:

感谢您提示过去的活动。在我意识到只有过去的事件之前,我从未收到任何事件.... :(

以上是关于Facebook SDK 4 for PHP - 无需登录抓取一些事件的主要内容,如果未能解决你的问题,请参考以下文章

使用 Facebook PHP SDK 4,但没有存储会话

在 laravel 中访问 facebook php sdk 4.0 和 Javascript sdk 上的 Token

PHP Facebook SDK 4.0 作为页面服务器发布到服务器

无法在 Laravel 4 中使用 Facebook 的 PHP SDK 获取用户个人资料

Facebook PHP SDK 4.0 使用 Lumen 登录

html ALL-IN-ONE - Facebook PHP SDK 4.0.x