我只想插入curl标记的特定字段:
我有一个变量,它将接收这个名为“$r”的标记,我使用foreach来捕获该值,但我没有得到它:答案如下
{
"access_token":"<TOKEN RESPONSE HERE >",
"token_type": "bearer",
"expires_in": 14400
}
下面是我的代码
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "refresh_token=<REFRESH HERE>&grant_type=refresh_token&client_id=<KEY HERE>&client_secret=<SECRET HERE>");
$body = curl_exec($ch);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
else{
if (is_array($result) || is_object($result))
{
foreach ($result as $r) {
echo $r['access_token'];
}
}
echo 'Foi: '.$result ;
}
curl_close($ch);
?>
1条答案
按热度按时间kuhbmx9i1#
curl_exec
返回一个字符串。用json_decode($result,1)
将其转换为关联数组