0

I have the same problem as here How to get results from Udemy Api with search? which unfortunately doesn't have an answer.

I'm able to retrieved the title etc, from this URL for example, https://www.udemy.com/api-2.0/courses/238934/?fields[course]=@all (you'll see the results in your browser) this URL works as well https://www.udemy.com/api-2.0/search-suggestions?q=java but not this one https://www.udemy.com/api-2.0/courses/?search=java

As mentioned here https://www.udemy.com/developers/affiliate/methods/get-courses-list/

get /api-2.0/courses/?search=java should work?

Here is my code:

$url = "https://www.udemy.com/api-2.0/courses/?search=java";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $c_id = base64_encode('XXX');
    $c_sid = base64_encode('XXX');

    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);

    curl_close($ch);

    $result = json_decode($result);

    $title = $result->title;

If someone could please shed some light on this.

Seb
  • 83
  • 1
  • 10
  • Light on what? You haven't told us a specific error or problem – ADyson Jan 20 '21 at 18:15
  • Thanks for your reply. This url doesn't return any results https://www.udemy.com/api-2.0/courses/?search=java while this one does for example: https://www.udemy.com/api-2.0/courses/238934/?fields[course]=@all (if you look at it in your browser). So there must be a way to query the Udemy feed without adding a specific course number like /238934/. – Seb Jan 20 '21 at 18:19
  • What do the response headers say? Does curl_error return anything? – ADyson Jan 20 '21 at 18:20
  • I got a 403 error with this URL https://www.udemy.com/api-2.0/courses/?search=java but a 200 with this one https://www.udemy.com/api-2.0/courses/238934/?fields%5Bcourse%5D=@all , not sure if that help. – Seb Jan 20 '21 at 18:31
  • I got 403 in the browser with that URL as well. Presumably you're supposed to send some credentials or have a valid login session in order to use it – ADyson Jan 20 '21 at 18:39
  • ok thanks, maybe my code is not working properly then. Although I can retrieve the title etc from this URL https://www.udemy.com/api-2.0/courses/238934/?fields[course]=@all. I'm not sure why the code would work with some URLs and not with some others. In general either one have access to an api or don't. – Seb Jan 20 '21 at 18:45
  • Not necessarily. It's up to the provider of the API. They can choose to make some data public and some not. It's their decision entirely. If there is documentation for this API you should be able to check how you are expected to authenticate with it, and which endpoints require authentication, and which don't. – ADyson Jan 20 '21 at 18:56
  • Thanks, but there are no clues in the doc. https://www.udemy.com/developers/affiliate/methods/get-courses-list/ – Seb Jan 20 '21 at 19:07
  • Not directly, although it does show you the authorisation headers. https://www.udemy.com/developers/affiliate/ explains it more clearly though. It seems you can _either_ send the client ID and secret in the form of the basic authentication protocol, _or_ send an Authorization header containing a base64 encoded combination of those two fields. You're doing neither. I don't know where you got the idea for the `X-Udemy-Client-Id` and `X-Udemy-Client-Secret` headers? And clearly `base64 encoded value of client-id:client-secret` is not the correct value for the authorization header. – ADyson Jan 20 '21 at 20:34
  • https://stackoverflow.com/a/2140445/5947043 shows how you do basic auth with cURL/PHP. And https://www.php.net/manual/en/function.base64-encode.php shows you how to base64-encode something - I think you know that already actually, but for some reason you've encoded the ID and secret separately, instead of as a single string, and you haven't used the value in the auth header. Now you just choose which approach to use...it doesn't seem to matter. – ADyson Jan 20 '21 at 20:38

1 Answers1

0

Based on ADyson comments, here is the answer (just update the code with your own clientID and clientSecret ID from Udemy):

I've basically updated the CURLOPT_HTTPHEADER:

Code:

    $url = "https://www.udemy.com/api-2.0/courses/?search=java";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);

//HTTP username.
$clientID = 'XXX';
//HTTP password.
$clientSecret = 'XXX';
//Create the headers array.
$headers = array(
    'Content-Type: application/json',
    'Authorization: Basic '. base64_encode("$clientID:$clientSecret")
);
//Set the headers that we want our cURL client to use.
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    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);

    curl_close($ch);

    $json = json_decode($result, true);
    
   // return $result; 
   foreach($json['results'] as $results) {

$title = $results['title'];
 
echo $title.'<br>';

}

Results: (Course titles based on this search: https://www.udemy.com/api-2.0/courses/?search=java)

Java Programming Masterclass for Software Developers

Java Programming for Complete Beginners

Java In-Depth: Become a Complete Java Engineer!

The Complete Java Certification Course

Etc.

Seb
  • 83
  • 1
  • 10