Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How to add HTTP Headers for authentication?

I want to add the HTTP headers for authenticating Udemy API access.Can someone point me as to how to add the headers.I already have the client id and secret key.I want to access the API from a wordpress theme.I can parse the JSON feed but i wanted to know as to how to add the HTTP Headers for authentication. https://developers.udemy.com/

Update: I tried this code but it still give me the unauthorized message.

$ch = curl_init($request);
curl_setopt($ch, CURLOPT_URL, $request);
$results= curl_exec($ch);
$str = file_get_contents('https://www.udemy.com/api-1.1/courses/5816/');
$json = json_decode($str, true); // decode the JSON into an associative array
print_r ($json);

Output: Array ( [error] => Array ( [code] => 401 [message] => Unauthorized - No Client [details] => ) )

1 Answer

You can use the CURL library. The CURLOPT_USERPWD option sets HTTP auth.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $results= curl_exec($ch);