du9826 2018-10-18 11:25
浏览 290
已采纳

从JSON php curl中提取数据:json_decode无法正常工作

I have the following code:

<?php 

$consumerKey = '';
$consumerSecret = '';
$url = '';

$data = array(
'grant_type'  => 'password',
    'username'    => '',
    'password'    => ''
);


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json','Accept-Language: en_US'));
curl_setopt($curl, CURLOPT_USERPWD, $consumerKey.':'.$consumerSecret);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    $result = json_decode($result);
    curl_close($curl);
?>

It returns the sample following json result, but not the access token despite the json_decode. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); does not return the token below. Thank you for helping me.

{"access_token":"ffdd8dfb-2013-32ee-bc3e-dc5689d6c8fb","refresh_token":"7bf1ddad-d696-3d83-a524-37dac002164a","scope":"default","token_type":"Bearer","expires_in":3600}
  • 写回答

1条回答 默认 最新

  • duan5801 2018-10-19 00:34
    关注

    Buoyed by your answers - @Andreas and @YvesLeBorg, I redid the code and got what I want. I am putting this to help someone else. Thank you.

    <?php 
    
    $consumerKey = '';
    $consumerSecret = '';
    $url = '';
    $curl = curl_init($url);
    $data = array(
        'grant_type'  => 'password',
        'username'    => '',
        'password'    => ''
    );
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json','Accept-Language: en_US'));
    curl_setopt($curl, CURLOPT_USERPWD, $consumerKey.':'.$consumerSecret);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
        $result = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $result = json_decode($result);
        $access_token = $result->access_token;
    echo $access_token;
        curl_close($curl);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 WSL上下载的joern在windows怎么用?
  • ¥17 MC9S12XS128单片机开发板实验
  • ¥15 jetson nano4GB
  • ¥15 电脑回复出厂设置,重装过程报错提示,求解决方案Windows 无法分析或处理无人参与应答文件 [C:\WINDOWS\Panther\unattend.xml,如何解决?
  • ¥15 进入lighttools中的UDOP编辑器的方法
  • ¥15 求Gen6d训练数据集
  • ¥20 liunx中winscp中可以登入ftp,但是不能登入sftp,如何解决
  • ¥15 lighttools的光学属性自定义的用法流程
  • ¥15 uni-app动态修改推荐页标题时报错
  • ¥15 图片是GET请求,后面是图片的id,Glide如何缓存?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部