9

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?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
ven
  • 395
  • 3
  • 9
  • 17
  • 1
    Why are you using `Content-Type` if your commandline example has a `X-abc-AUTH:` header? – mario Nov 02 '12 at 00:20
  • I was getting a "content type required" error. But I just figure it out! I've updated the code above. – ven Nov 02 '12 at 00:27

4 Answers4

20

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 :)

sergiocruz
  • 652
  • 5
  • 10
  • I'm struggeling with the problem that only the first array item will be received by the server. Maybe somebody can help with this https://stackoverflow.com/questions/49771245/php-rest-curl-authentication-header-items-missing ? – Perino Apr 11 '18 at 12:11
  • I used this for the PHP/curl auth header for AWS AppSync GraphQL. Thanks for the help boys. 'x-api-key: '.$authToken – smack-a-bro Feb 13 '20 at 20:21
3

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 ;
Dadaso Zanzane
  • 6,039
  • 1
  • 25
  • 25
0
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));
deb0rian
  • 966
  • 1
  • 13
  • 37
  • Then why the same post above got + and I've got a - ? :) – deb0rian Nov 02 '12 at 00:30
  • This almost worked! This made it work: curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth)); – ven Nov 02 '12 at 00:41
  • well,yes, I just gave an idea how to send custom http headers, wasn't going for a 100% working solution, just the syntax. Not sure why I got minuses :( – deb0rian Nov 02 '12 at 00:43
0

You set only one request header, not the two you wanted. You could do it for example like this:

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

// url
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet));

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

// curl init
$ch = curl_init($service_url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLINFO_HEADER_OUT    => true,
    CURLOPT_HTTPHEADER     => [
        $contentType,
        $auth,
    ],
]);

// curl exec
$data = curl_exec($ch);
curl_close($ch);

// output
echo $data;

(change the service url to the right one to get this to work)

hakre
  • 193,403
  • 52
  • 435
  • 836
  • thanks for your suggestion - seems like it would have worked but it didn't :( I change the service url + key too...thanks again. – ven Nov 02 '12 at 00:38
  • You might want to do some error checking after `curl_exec`: `var_dump(curl_error($ch));` – hakre Nov 02 '12 at 00:40