1

My code to add header is:

$apikey="xyz";
     curl_setopt($handle, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'Accept: application/json',
                                            'X-Auth-Token: $apikey'                                 
                                            ));

But I am getting an error showing that **This request requires HTTP Authentication.**Now I want to know whether I am right? If Yes why I am getting error.If I am wrong how can do this..

Thankyou,

2
  • try adding this: curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); Commented Jan 10, 2015 at 9:55
  • This is not working...I am getting same error again...Is there any other option??? Commented Jan 10, 2015 at 10:01

2 Answers 2

10

Use double quotes if you want your variable to be interpolated by PHP, i.e. "X-Auth-Token: $apikey".

For more info see PHP: Strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Or you can do 'X-Auth-Token: '.$apikey
0
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http:www.example.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('name' => 'Bhuvanagiri Gireesh') );
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Accept: application/json';
    $headers[] = 'key: license-key ';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

There are many ways to achieve it but, this is the easiest way to add headers in curl

Comments

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.