19

I am new to PHP. I am trying to get the Header from the response after sending the php curl POST request. The client sends the request to the server and server sends back the response with Header. Here is how I sent my POST request.

   $client = curl_init($url);  
   curl_setopt($client, CURLOPT_CUSTOMREQUEST, "POST");
   curl_setopt($client, CURLOPT_POSTFIELDS, $data_string);
   curl_setopt($client, CURLOPT_HEADER, 1);
   $response = curl_exec($client);
   var_dump($response);

Here is the Header response from server that I get from the browser

HTTP/1.1 200 OK 
Date: Wed, 01 Feb 2017 11:40:59 GMT 
Authorization: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2Vycy9CYW9CaW5oMTEwMiIsIm5hbWUiOiJhZG1pbiIsInBhc3N3b3JkIjoiMTIzNCJ9.kIGghbKQtMowjUZ6g62KirdfDUA_HtmW-wjqc3ROXjc Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Server: Jetty(9.3.6.v20151106) 

How can I extract the Authorization part from the Header ?. I need to store it in the cookies

Tuan Dinh
  • 407
  • 1
  • 3
  • 13

4 Answers4

39

It converts all headers into an array

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "example.com");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

$headers = [];
$output = rtrim($output);
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);

foreach($data as $part){

    //some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
    $middle = explode(":",$part,2);

    //Supress warning message if $middle[1] does not exist, Thanks to @crayons
    if ( !isset($middle[1]) ) { $middle[1] = null; }

    $headers[trim($middle[0])] = trim($middle[1]);
}

// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • Good answer, although you don't account for trailing newlines, or null values. You may get warnings: `Notice: Undefined offset: 1`. I'm going to suggest an edit for review. – Crayons May 16 '18 at 18:06
  • 6
    My edit was rejected as @user75ponic believes it is "superfluous". Sorry, but he's wrong. @Thamaraiselvam As my previous comment stated, your code does not account for trailing newlines which can, and often does, exist in header returns. The existence of a trailing newline causes this code to break entirely. Use `rtrim($output)` to fix that. You also don't handle null values at all and you will receive PHP warnings should any header contain a null value, again, a possibility. You can handle this like so: `if ( !isset($middle[1]) ) { $middle[1] = null; }`. – Crayons May 20 '18 at 08:37
  • @Crayons I will check and do the necessary changes +1 – Thamaraiselvam Oct 12 '18 at 07:14
  • 1
    I suggest also to replace $middle=explode(":",$part); with $middle=explode(":",$part,2); because some headers will contain ":" character (Location for example), and the part after ":" will be lost. – Emanuele Nov 19 '18 at 10:00
  • @Crayons Thanks for your inputs and answer updated. – Thamaraiselvam May 29 '19 at 06:15
  • @Emanuele Thanks for your inputs and answer updated. – Thamaraiselvam May 29 '19 at 06:15
3

For the first answer note that the code:

$middle=explode(":",$part);

will produce wrong results with string data containing :, like for example:

Sat, 14 Jan 2017 01:10:01 GMT

The correct code to split the fields to build the array would be like:

$middle=explode(":",$part,2);
Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
-1

You just include this coding into your curl request

curl_setopt($curl_exec, CURLOPT_HEADER, true); 
curl_setopt($curl_exec, CURLOPT_NOBODY, true);

after your curl execution use $header_data= curl_getinfo($curl_exec);

Then you get all the headers

print_r($header_data);

or use the shell_exec

echo shell_exec("curl -I http://example.com ");
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
  • 7
    This gets the header of the request rather than the header of the response. – Adam Nierzad Jul 14 '17 at 08:52
  • 3
    `curl_getinfo()` still doesn't return the response headers when you use the above code. But setting `CURLOPT_HEADER` and `CURLOPT_NOBODY` *does* make `curl_exec()` return *only* the response headers. Which is maybe useful if that's what you want, but the problem then is you only got the response headers and not the response body, and usually you want both. – orrd Aug 21 '19 at 18:42
-4

just use simple code to convert curl response to array.

 $response  = curl_exec($ch);
 $header_data= curl_getinfo($ch);
 print_r($header_data);
Sourav
  • 113
  • 3