How to get results from Udemy Api with search?

こ雲淡風輕ζ 提交于 2021-01-29 11:30:04

问题


I want use udemy api in my website with search field in api. Udemy gives my this url: /api-2.0/courses/?search=mysql which when I use I'm getting an empty array in my results.

However, when I use this url: /api-2.0/courses/ It works fine, and I'm getting 1000 rows of course details..

Please help me out with the search option, how do I make it work?

<?php
echo "API testing is going on\n";


function callAPI($method, $url, $data){
   $curl = curl_init();

   switch ($method){
      case "POST":
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         break;
      case "PUT":
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
         break;
      default:
         if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
   }

   // OPTIONS:
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(   'Accept: 
application/json, text/plain, */*',
          'Content-Type: application/json;charset=utf-8',    
        'Authorization: xxx',
   ));
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

   // EXECUTE:
   $result = curl_exec($curl);
   if(!$result){die("Connection Failure");}
   curl_close($curl);
   return $result;
}

$URL="https://www.udemy.com/api-2.0/courses?search=mysql',auth= 
   ('xxclientidxx','xxclientsecretxx')'";

$get_data = callAPI('GET',$URL,false);
$response = json_decode($get_data, true);

$file_json=$response['results'];
$fp=fopen('results.json','w');
fwrite($fp,json_encode($response));
fclose($fp);

echo '<br>';


print_r($response['results']);

?>

回答1:


It would be good if you can check the API docs available from Udemy. Swagger API platform seems available for them.

Please check the URL for the courses API: https://www.udemy.com/developers/affiliate/methods/get-courses-list/



来源:https://stackoverflow.com/questions/56661604/how-to-get-results-from-udemy-api-with-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!