Richard K Maleho
Richard K Maleho

Reputation: 392

Getting users Facebook friends with php throws exception

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

Upvotes: 0

Views: 260

Answers (1)

andyrandy
andyrandy

Reputation: 73984

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

Upvotes: 1

Related Questions