Monday, October 10, 2022

[SOLVED] curl request in udemy api is not working

Issue

I'm trying to get the udemy course catalog by making a request to udemy api.But all its returning is bool(false).
Here is my code:

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

Solution

My favorite solution:

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

?>


Answered By - Magnotta
Answer Checked By - Marie Seifert (WPSolving Admin)