【问题标题】:Udemy Affiliate API - Authentication Error (403 Notice Array to string conversion)Udemy Affiliate API - 身份验证错误(403 通知数组到字符串的转换)
【发布时间】:2020-09-07 23:32:05
【问题描述】:

我正在尝试将 Udemy API 与 Stackoverflow 帖子中的正确答案一起使用:curl request in udemy api is not working

header('Content-Type: application/json');
$url = "https://www.udemy.com/api-2.0/courses";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: xxx','X-Udemy-Client-Secret: xxx',"Authorization: base64 encoded value of client-id:client-secret","Accept: application/json, text/plain, */*"));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result=curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
echo curl_getinfo($ch,CURLINFO_HTTP_CODE);
// Closing
curl_close($ch);

// Will dump a beauty json :3
echo $result = json_decode($result,true);

我添加了自己的客户端 ID 和机密 ID,并在 Udemy 网站上对其进行了测试,状态为 200。

我从上面的代码得到的错误是:403 Notice Array to string conversion.

谁能看到我错过了什么?

【问题讨论】:

    标签: php curl oauth-2.0 php-curl


    【解决方案1】:
    echo $result = json_decode($result,true);
    

    json_decode() 将响应(包含 JSON 的字符串)转换为关联数组。

    然后将数组传递给echo,这会引发错误。

    修复:

    echo $result; // `$result` is still a string (the response)
    

    那么如果你需要以关联数组的形式访问响应

    $result = json_decode($result,true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2017-01-31
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多