我正在使用PHP Curl lib来发出一些REST API请求。我的代码如下:
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host."/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic '.$token.''
));
// execute the request
$output = curl_exec($ch);
// output the authorization information - includes the header
echo($output) . PHP_EOL;
响应如下:
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Frame-Options: DENY Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Cache-Control: no-store Pragma: no-cache Content-Type: application/json;charset=UTF-8 Content-Length: 498 Date: Tue, 11 Aug 2015 10:38:25 GMT {"access_token":"access_token_here","token_type":"bearer","expires_in":43199,"scope":"read write","jti":"jti_code_here"}
我唯一需要的是access_token
,我怎么试着编辑我的$ output
我知道去访问令牌的唯一方法就是取消一些字符,有没有办法删除标题和使用响应的其余部分作为数组或json或我可以轻松访问的东西?
感谢您的任何建议。