简体   繁体   中英

Facebook Graph API: having issue with fetching Json data (Access Token) using curl

I am trying to get the access token using curl.

$tokenurl = "https://graph.facebook.com/oauth/access_token?client_id=11111111111111&redirect_uri="http://www.example.com/?callback"&client_secret=11111111111111111111111111111&code=" . $_GET['code'];


$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, $tokenurl );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );

//var_dump(curl_exec($_h));

$result = curl_exec($_h);
$json = json_decode($result, true);
echo $json['access_token'];

curl_close($_h);

return $result;

var_dump is showing valid data, but I can't fetch it using json_decode() . echo $json['access_token']; shows nothing. Am I doing something wrong? Any suggestion would be helpful.

var_dump is showing this data:

string(733) "HTTP/1.1 200 OK Access-Control-Allow-Origin: * Pragma: no-cache Cache-Control: private, no-cache, no-store, must-revalidate x-fb-rev: 3044559 Content-Type: application/json; charset=UTF-8 x-fb-trace-id: E9niQhqkk34 facebook-api-version: v2.3 Expires: Sat, 01 Jan 2000 00:00:00 GMT Vary: Accept-Encoding X-FB-Debug: 8y64Y0AJvl8YiFFk+kQj8pVvJHBQJLBPC854l5J7e41ypLTYesLTYvfMjsc+FjH9mpZw4Fi7ZITONGM8sazlCw== Date: Thu, 25 May 2017 10:01:35 GMT Transfer-Encoding: chunked Connection: keep-alive {"access_token":"EAAN12SVnRf8BAGGcZBFhAn8Pz7JHqsLVSC00pkZA81ap4rZAJOwSl3ZABaQPT7L03vPSNnsGS2lTduSN1FQMy1q8vRqiefD0sCd3sN4wu1n9tuMgMqXCeVi5zAKod1oPrjgJA246VHN5qkOxQGZBj52ZCgWblJ877cZD","token_type":"bearer","expires_in":5118394}" 0

var_dump is showing valid data, but I can't fetch it using json_decode(). echo $json['access_token']; shows nothing. Am I doing something wrong?

Yes - you are trying to decode the whole response, headers and body, as JSON.

Only the body contains the JSON data you are interested in - so don't set CURLOPT_HEADER.

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