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 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下