首页 > 解决方案 > Udemy Affiliate API - 身份验证错误(403 通知数组到字符串的转换)

问题描述

我正在尝试将 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。

谁能看到我错过了什么?

标签: phpcurloauth-2.0php-curl

解决方案


echo $result = json_decode($result,true);

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

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

修复:

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

然后,如果您需要以关联数组的形式访问响应

$result = json_decode($result,true);

推荐阅读