Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending auth in headers php curl

trying to do the equivalent of this in PHP - and failing :):

curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;

"123456789" is the API key. The command line statement works fine.

PHP code (does not work):

$urlToGet = "http://www.cnn.com";
$service_url = "http://APIserviceProvider=$urlToGet";

//header

 $contentType = 'text/xml';          //probably not needed
 $method = 'POST';                   //probably not needed
 $auth = 'X-abc-AUTH: 123456789';    //API Key

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

//does not work



// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' . 
   // $contentType . '; auth=' . $auth));

    //works!   (THANKS @Fratyr for the clue):

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));

//this works too (THANKS @sergiocruz):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Some_custom_header: 0',
  'Another_custom_header: 143444,12'
));


//exec

$data = curl_exec($ch);
echo $data;
curl_close($ch);

Any ideas?

like image 726
ven Avatar asked Nov 02 '12 00:11

ven


People also ask

How do I pass the authorization header in cURL?

Alternatively, you can pass the basic auth credentials using the Curl -H "Authorization: Basic [token]" command-line option. The -H parameter passes the authorization header to Curl like any other custom header, without any processing.

How do I submit auth token in header?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

How do you pass the authorization Bearer Token in cURL?

Sending the Bearer Token with a Curl POST request is similar to sending the Bearer Token with a Curl GET request. POST data is passed with the -d command-line option, and the authorization header and the bearer token are passed with the -H command-line option.

How do I make a request using HTTP basic authentication with PHP cURL?

To post a Curl request with Basic Authorization credentials, you can use the -u (or --user) command line parameter: --user username: password.


2 Answers

In order to get custom headers into your curl you should do something like the following:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Some_custom_header: 0',
  'Another_custom_header: 143444,12'
));

Therefore the following should work in your case (given X-abc-AUTH is the only header you need to send over):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-abc-AUTH: 123456789' // you can replace this with your $auth variable
));

If you need additional custom headers, all you have to do is add on to the array within the curl_setopt.

I hope this helps :)

like image 115
sergiocruz Avatar answered Oct 19 '22 04:10

sergiocruz


Use the following Syntax

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = array();
$headers[] = 'X-abc-AUTH: 123456789';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$headers[] = 'Host: 202.71.152.126';
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
$headers[] = 'X-MicrosoftAjax: Delta=true';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;
like image 31
Dadaso Zanzane Avatar answered Oct 19 '22 03:10

Dadaso Zanzane