dongshanfan1941 2014-05-05 20:04
浏览 30
已采纳

Facebook SDK V4

I am trying to convert my application to the latest version of Facebooks PHP SDK (V4). I am having three issues: The first being that every call (using my code) requires to go through the getLoginUrl() function EVERY time. This seems strange to me, as the point of the session is to not have to generate a new code for every call, correct? Second, I'm trying to figure out how to access the data - everything is now wrapped in a ["backingData":protected] node... for which, when I try to access... gives me a Fatal Error:

 Cannot access protected property Facebook\GraphObject::$backingData

Finally, I'm assuming I get this error because I haven't requested the correct permissions. As you will see in my code, I am using the previous 'scope' array syntax as a parameter in the getLoginUrl() function... but it never pops up and asks me to accept permissions.

Here is my code:

require_once( 'fb2/FacebookSession.php' );
require_once( 'fb2/FacebookRedirectLoginHelper.php' );
require_once( 'fb2/FacebookRequest.php' );
require_once( 'fb2/FacebookResponse.php' );
require_once( 'fb2/FacebookSDKException.php' );
require_once( 'fb2/FacebookRequestException.php' );
require_once( 'fb2/FacebookAuthorizationException.php' );
require_once( 'fb2/GraphObject.php' );
require_once( 'fb2/FacebookOtherException.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\FacebookOtherException;

session_start();

FacebookSession::setDefaultApplication('522464134491111', '<removed>');

$helper = new FacebookRedirectLoginHelper('https://mediaalias.com/fb_apps/graph_search_2.php/');

try {
    $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {

} catch(\Exception $ex) {

}

if ( isset( $session ) && $session ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/breakthelimit/posts' );
  $response = $request->execute();
  // get response
  $posts = $response->getGraphObject();

  // print data
  echo "<pre>";
    var_dump($posts);
  echo "</pre>";
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl(array('scope' => 'manage_pages, read_insights')) . '">Login</a>';
}

I am trying to fetch the ID's of each of my pages posts. Previously, this was super simple.

Here is what my response looks like from the API:

object(Facebook\GraphObject)#289 (1) {
  ["backingData":protected]=>
  array(2) {
    ["data"]=>
    array(20) {
      [0]=>
      object(stdClass)#5 (18) {
        ["id"]=>
        string(31) "213418912041722_602369633146646"
        ["from"]=>
        object(stdClass)#6 (3) {
          ["category"]=>
          string(7) "Website"
          ["name"]=>
          string(20) "Favorite Exotic Car?"
          ["id"]=>
          string(15) "213418912041722"
        }

Again, the issue for me, is that every time I refresh my script, the code has to generate a new code (token? - it says ?code=) on every refresh of my script. And, how to I access the data in the response?

I have tried dumping:

$posts->backingData['data'] 

Which is where I get my fatal error. And just going straight to:

$posts->data

returns NULL.

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dqwd71332 2014-05-05 20:48
    关注

    Couple things to address:

    Once you get a session from the redirect, save it. Use a session variable for instance.

    There are methods on FacebookRequest for getting the response data, like getResponse and getRawResponse.. or GraphObject has ->asArray() to get the backing data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示