【问题标题】:curl request in udemy api is not workingudemy api 中的 curl 请求不起作用
【发布时间】:2016-11-04 19:31:55
【问题描述】:

我正在尝试通过向 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);

【问题讨论】:

    标签: php curl


    【解决方案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);
    

    【讨论】:

    • 仍然不工作,它给出相同的输出,即bool(false)
    【解决方案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);
    
    ?>
    

    【讨论】:

    • 非常感谢@parvez。一段时间以来,我一直在努力理解与 Udemy API 的连接!这非常有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多