我正在尝试将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注意数组到字符串的转换。
谁能看到我想念的东西吗?
echo $result = json_decode($result,true);
[json_decode()
将响应(包含JSON的字符串)转换为关联数组。
然后将数组传递给echo
,这会引发错误。
解决方法:
echo $result; // `$result` is still a string (the response)
然后,如果您需要作为关联数组访问响应
$result = json_decode($result,true);