问题描述
我正尝试使用其会员API和PHP来访问udemy课程列表。
如果不使用json_decode,我可以毫无问题地获取数据。 当我使用json_decode我收到此错误。
'SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符'
function getcourse($id)
{
header('Content-Type: application/json');
$url = "https://www.udemy.com/api-2.0/courses/$id?fields[course]=@all";
// 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
$c_id = base64_encode('myclientid');
$c_sid = base64_encode('myclientsecretid');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: '.$c_id.'','X-Udemy-Client-Secret: '.$c_sid.'',"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);
$result = json_decode($result);
return $result;
}
$cdetail = getcourse(120042);
print_r($cdetail);
1楼
Lucas
1
已采纳
2019-02-12 12:38:24
运行您的代码没有任何错误。 您收到此错误
SyntaxError:JSON.parse:JSON数据的第1行第1列中出现意外字符
因为您使用的是“ header('Content-Type:application / json');”,并且您可能使用的是Firefox,它将认为所有内容都是json,并且将验证其是否有问题。
注释“ header('Content-Type:application / json');” 您将获得数据。