douzhuang6321 2016-09-08 12:40
浏览 42

当我尝试解码来自youtube api的响应时,json_decode返回null

So I try to create a oauth connection with youtube api but I have a problem with decoding the response. My problem is here:

public function getCallbackUrl($code)
{

    // Grab the returned code and extract the access token.
    $this->params = [
        'code'          => $code,
        'client_id'     => '#####',
        'client_secret' => '####',
        'redirect_uri'  => '######',
        'grant_type'    => 'authorization_code'
    ];

    // Get access token
    $command = 'curl --data "' . http_build_query($this->params) . '" https://accounts.google.com/o/oauth2/token';
    exec($command, $resultToken);
    $resultToken = json_decode($resultToken[0]);

After I make the exec $resultToken returns like this:

array(5) { [0]=> string(1) "{" [1]=> string(93) " "access_token" : "###########"," [2]=> string(26) " "token_type" : "Bearer"," [3]=> string(21) " "expires_in" : 3599" [4]=> string(1) "}" }

But when I try to json_decode to obtain an array: $resultToken = json_decode($resultToken[0]); I get a result NULL Can anyone explain to me why please? Any help is welcomed, thank you for your time!

var_dump($resultToken[0]) = NULL;
  • 写回答

1条回答 默认 最新

  • duanbiao4035 2016-09-08 12:51
    关注

    You are trying to decode a value which is not valid JSON. Verify whether your API request is returning the correct response. From the documentation:

    NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    On a side note, you are using a system call to invoke cURL. Consider using the native PHP cURL extension or a proper library.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度