質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top