从parse_str获取变量

时间:2017-03-28 17:12:05

标签: php facebook facebook-graph-api

我正在尝试从facebook图表中读取回复。它发给我access_token但我似乎无法阅读它。这曾经起作用,但出于某种原因,它今天停止了工作。

$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));

上面的print_r()显示:

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

但$ params [&#39; access_token&#39;]始终为空。如何正确获取access_token?

感谢

1 个答案:

答案 0 :(得分:1)

$response = file_get_contents($token_url);

此处{J}对象中的$response,您必须使用json_decode()将其转换为如下数组:

$resp = json_decode($response, true);

并使用$resp之类的:

echo $resp['access_token'];