PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Getting users Facebook friends with php throws exception

 February 04, 2022     facebook-php-sdk, php     No comments   

Issue

I'm trying to get a list of the users friends with php but it keeps throwing an exception the code is the same from Facebook for developers docs https://developers.facebook.com/docs/graph-api/reference/v3.0/user/friends

here is my code

$fb = new \Facebook\Facebook([
        'app_id' => APP_ID,
        'app_secret' => APP_SECRET,
        'default_graph_version' => DEFAULT_GRAPH_VERSION,
        //'access_token' => '{'.$fbToken.'}', // optional
    ]);

try {
            $response = $fb->get('/friends', $fbToken);
        } catch(FacebookExceptionsFacebookResponseException $e) {
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch(FacebookExceptionsFacebookSDKException $e) {
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

        $graphNode = $response->getGraphNode();

And here is the stacktrace of the error

[03-Jul-2018 07:29:33 UTC] PHP Fatal error:  Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'Unable to convert response from Graph to a GraphNode because the response looks like a GraphEdge. Try using GraphNodeFactory::makeGraphEdge() instead.' in /facebook/Facebook/GraphNodes/GraphNodeFactory.php:224
Stack trace:
#0 /facebook/Facebook/GraphNodes/GraphNodeFactory.php(93): Facebook\GraphNodes\GraphNodeFactory->validateResponseCastableAsGraphNode()
#1 /facebook/Facebook/FacebookResponse.php(289): Facebook\GraphNodes\GraphNodeFactory->makeGraphNode(NULL)
#2 {main}
  thrown in /facebook/Facebook/GraphNodes/GraphNodeFactory.php on line 224

Solution

Try this instead:

$graphNode = $response->getGraphEdge();

Quite old so i did not mark it as duplicate: FB Graph API query doesn't work in PHP SDK



Answered By - andyrandy
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing