我正在尝试通过向udemy api提出请求来获取udemy课程目录。但它返回的所有内容都是bool(false)
。
这是我的代码:
$ch = curl_init();
$headers = array(
'$udemy_client_id = xxx',
'$udemy_client_secret = xxxxx',
'Accept: application/json, text/plain, */*'
);
curl_setopt($ch, CURLOPT_URL, 'https://www.udemy.com/api-2.0/courses');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
答案 0 :(得分:2)
正确答案:
<?php
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);
?>
答案 1 :(得分:1)
试试这个
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: {YourID}','X-Udemy-Client-Secret: {YourSecret}'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$results= curl_exec($ch);
var_dump($results);
答案 2 :(得分:1)
我认为这会对你有所帮助
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: {YourID}','X-Udemy-Client-Secret: {YourSecret}'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$results= curl_exec($ch);
echo $results;
请参阅此帖子了解更多详情:How to add curl HTTP Headers for authentication in PHP?