1

i am trying to read the responde from facebook graph. It send me the access_token but i cant seem to read it. This used to work but for some reason it stopped working today.

$token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $fb_app_id . "&redirect_uri=" . urlencode($fb_return_url) . "&client_secret=" . $fb_app_secret . "&code=" . $code;

$response = file_get_contents($token_url);

$params = null;
parse_str($response, $params);

echo "<pre>";
print_r($params);
echo "</pre>";

$_SESSION['access_token'] = $params['access_token'];

$graph_url              = "https://graph.facebook.com/me?access_token=" . $params['access_token'];
$facebook_user_info     = json_decode(file_get_contents($graph_url));

The above print_r() shows:

Array
(
    [{"access_token":"EAAXZBwzyN6xkBAD0szEptWK6tMJlH68HmmZA6SM2nZBZCoQgPIDAvNaL0Fv9z89nwJhkkRCNRJHvlS9rzn09EdUEbH9ZBVajZAVXlF0SJyoJhW3NvMZAyZB7MOMSbFEsQe4RKSTxiQIZCVxnMPYI3tSxsjxgVZBPZCrbAZD","token_type":"bearer","expires_in":5162746}] => 
)

but $params['access_token'] is always empty. How to properly get the access_token?

thanks

Joan Silverstone
  • 385
  • 1
  • 3
  • 15

1 Answers1

1
$response = file_get_contents($token_url);

here $response in an JSON object, you have to use json_decode() to convert it into an array like:

$resp = json_decode($response, true);

and use $resp like:

echo $resp['access_token'];
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59