0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.