简体   繁体   中英

Facebook PHP SDK returning undefined

I'm trying to print out the name of my own personal profile on a span. I'm using the Facebook PHP SDK V5.x and I'm using my app's id and secret and an access token for my page that I manage that I gave permissions for.

Here is the print out code:

<?php echo $user['name']; ?>

Here is the code I am using to try to extract the user name:

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.12',
  'default_access_token' => '{access-token}', // optional
 ]);

try {

$response = $fb->get('/me?fields=id,name', '{access-token}');
$user = $response->getGraphUser();

It just says "undefined" though. Why? I'm not getting any kind of fatal error.

Try this code:

after setting the fb id,secret ect

try {
    $accessToken = $helper->getAccessToken();
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    echo "Response Exception: " . $e->getMessage();
    exit();
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    echo "SDK Exception: " . $e->getMessage();
    exit();
}

$oAuth2Client = $FB->getOAuth2Client();
    if (!$accessToken->isLongLived())
        $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);

    $response = $FB->get("/me?fields=id, first_name, last_name, email,)", $accessToken);
    $userData = $response->getGraphNode()->asArray();
    $userData = $response->getDecodedBody();
    echo "<pre>";
    var_dump($userData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM