0

I'm trying to curl a page (server.php) and send it three variables in a header ($webServiceId $hash and $timestamp), shown below. How can I grab the two variables out of the header in server.php so I can process them and send a response? I've tried googling and searching here, but I can't seem to find out how.

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'server.php');
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: URL-Encoded-API-Key {$webServiceId},{$hash},{$timestamp}"));
$response = curl_exec($ch);
curl_close ($ch);
// dump response
print_r( $response );

If server.php echos the headers its recieved back through the response, the print_r of $response yields:

HTTP/1.1 200 OK Date: Thu, 02 Aug 2012 22:18:59 GMT Server: LiteSpeed Connection: close X-Powered-By: PHP/5.3.14 Allow: GET

the Authorization: URL-Encoded-API-Key is't in there. Could I be setting up the curl header wrong?

Derek
  • 97
  • 1
  • 1
  • 11

3 Answers3

0

The function getallheaders() will return the complete request headers. If you call it from server.php you should be able to receive the custom headers sent with cURL.

http://php.net/manual/en/function.getallheaders.php

EDIT

By the way, you need quotes around server.php:

curl_setopt($ch, CURLOPT_URL, 'server.php');

EDIT 2

I tested it using 2 scripts:

client.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/server.php');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: URL-Encoded-API-Key 12,12113132,1330032113"));
$response = curl_exec($ch);
curl_close ($ch);

// dump response
var_dump( $response );

server.php

var_dump(getallheaders());

When calling client.php I get the following response:

array(3) {
  ["Host"]=>
  string(9) "127.0.0.1"
  ["Accept"]=>
  string(3) "*/*"
  ["Authorization"]=>
  string(42) "URL-Encoded-API-Key 12,12113132,1330032113"
}

My guess is your custom header is incorrectly formatted. Try using a simple header and see if it works:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: 123456"));
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • I tried using that, but it returns everything except what I'm looking for. Am I setting up the curl wrong above? (I know what its returning because server.php is echoing the getallheaders() function back into the response.) – Derek Aug 02 '12 at 22:23
  • @Derek I updated my answer with the results of my test. Try my suggestion. – Tchoupi Aug 03 '12 at 14:00
0

I've never used getallheaders() but the documentation says it reads the headers for the current request, which is not what you are doing.

The answer is here: Can PHP cURL retrieve response headers AND body in a single request?

After you get all the headers as a string, then explode it, just go through each with a strstr() or regex to get the values.

Community
  • 1
  • 1
Michael Ozeryansky
  • 7,204
  • 4
  • 49
  • 61
  • Actually I am trying to get the headers for the current request. Through curl I am trying to send an extra custom header to the php page I am cURLing and that page needs to retrieve the headers and process them. – Derek Aug 02 '12 at 22:21
  • Now it seems your question isn't worded correctly. When you say "curl a page" I assume you are using curl to request and download it. Then send it back? – Michael Ozeryansky Aug 02 '12 at 22:24
  • @MichaelOzeryansky I might be wrong, but I understood that Derek needs to handle both end of the request. The script provided is used to send a request with extra headers to `server.php`. And he needs to retrieve those headers in `server.php`, which is where `getallheaders()` comes handy. – Tchoupi Aug 03 '12 at 13:31
0

You're doing it wrong. "CURLOPT_HEADER" is just a bool to show request and response headers in the output. You need to be using "CURLOPT_HTTPHEADER", which expects an array of headers and is used to pass headers with the request.

Look at: http://www.php.net/manual/en/function.curl-setopt.php

Michael Ozeryansky
  • 7,204
  • 4
  • 49
  • 61