0

I want to add the HTTP headers for authenticating Udemy API access.Can someone tell 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 PHP page. https://developers.udemy.com/ Here is the code i tried using:

$ch = curl_init($request);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:MY_ID','X-Udemy-Client-Secret:Secret'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$results= curl_exec($ch);
echo $results;

Output:

Blank Page

Can someone point out what the problem might be?

4
  • 1
    Why are you running a curl request and then file_get_contents? echo $results; Commented Aug 20, 2014 at 14:38
  • @drmarvelous I basically want to access the API feed which is in json format in a wordpress theme.But it is not authenticated. Commented Aug 20, 2014 at 14:40
  • 1
    Correct, but you're already doing that with curl. Right now you are performing TWO different requests (albeit mal-formed) Commented Aug 20, 2014 at 14:41
  • I did that and it gives me a blank page. Commented Aug 20, 2014 at 14:47

1 Answer 1

2

As @drmarvelous wrote, you perform two requests (1st - by CURL, and 2nd - by file_get_contents) which does the same. Wherein the result of CURL request is not actually used in your script. It use the result of file_get_contents request which is performed without authentication parameters. Because of this you getting Unauthorized error.

So you have to use the result of CURL request:

...
$json = json_decode($results, true);
print_r($json);

Update:

You have to ensure you use valid URL for API request, i.e. value of $request in your code should be valid URL. Also, ensure you pass valid authentication parameters (Client-Id and Client-Secret) by HTTP headers.

Furthermore, since API is secured, you have to disable SSL peer verification by setting CURLOPT_SSL_VERIFYPEER option to false.

So the code should look like this:

$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);
echo $results;
Sign up to request clarification or add additional context in comments.

7 Comments

Hey i tried making those changes and have updated the post but when i use echo $results; It gives a blank page.
But what you've expected? Some message? I think that a blank page is a normal response to authentication request. I.e. "no messages" means "OK" (no errors).
Oh i was expecting a feed as shown on the api page.I want to access the feed data.
You cannot get data on authentication request. It's only for authenticating user, not for getting data
Even if i enter a wrong API secret key it shows a blank page.For accessing the data after authentication what do i have to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.