Process Rest response php

时间:2017-12-18 06:37:51

标签: php rest token access

I was calling a site to get access token. Here is the response How can I get the actual token using php.

HTTP/1.1 200 OK Date: Sat, 16 Dec 2017 16:54:20 GMT
Content-Type: application/json;
charset=UTF-8
Content-Length: 113
Connection: keep-alive
Cache-Control: no-store
Server: Apigee Router { "access_token": "1NQA27eHVzHC6idx2f7JDbJiD4CQ", "expires_in": "3599" }

The following code fails.

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$credentials)); //setting a custom header
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$curl_response = curl_exec($curl);
$token = $curl_response['access_token'];
echo "<br/>";
echo $token;

1 个答案:

答案 0 :(得分:0)

You need to decode JSON before accessing value.

$curl_response = curl_exec($curl);
curl_close ($curl);
$data = json_decode($curl_response,true); 
$token = $data['access_token'];
echo "<br/>";
echo $token;