php - 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。
谁能看到我错过了什么?
解决方案
echo $result = json_decode($result,true);
json_decode()
将响应(包含 JSON 的字符串)转换为关联数组。
然后将数组传递给echo
,这会引发错误。
修复:
echo $result; // `$result` is still a string (the response)
然后,如果您需要以关联数组的形式访问响应
$result = json_decode($result,true);
推荐阅读
- javascript - 胡萝卜搜索 > 3 个级别
- python - 如何使用 Plotly 的 Dash 更改 dcc.Input 的占位符字体颜色
- java - 国际音标 (IPA) 的 Java JDBC 问题
- c++ - 在编译时检查三向比较运算符支持
- algorithm - 在图像上运行算法的工具
- r - 在密度图中添加多条虚线
- vb.net - 从 .bin 文件中的文本文件中查找特定字符串?
- xcode - 从 Github 向“框架、库和嵌入式内容”添加包时,如何指定版本?
- pointers - trie中每个节点26个指针?
- python - 如何从当前时间减去 60 秒并以“时:分:秒”格式显示?