简体   繁体   中英

How to grab headers when cURLing a page

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?

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"));

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM