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
There is a simple solution built into cURL if you are only interested in the response headers and not the content of the body. By setting the CURLOPT_HEADER and CURLOPT_NOBODY options to true, the result of curl_exec() will only contain the headers.
We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers .
Just use the below piece of code to get the response from restful web service url, I use social mention url. Oldie bug a goodie... +1 for using curl_setopt_array(). So much cleaner than calling curl_setopt() over and over.
curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.
If you set the curl option CURLOPT_FOLLOWLOCATION, cURL will follow the location redirect for you. If you want to get the headers, set the option CURLOPT_HEADER to 1, and the HTTP response you get back from curl_exec () will contain the headers.
PHP: Response Headers (cURL) Getting the HTTP response headers with cURL in PHP is not straight forward. There is no build-in way to do this, but we can still cut out the headers from the response message, if CURLOPT_HEADER is true. By. When a HTTP request has been received by a server, typically a response is sent back to the client.
Custom request headers can also be defined when using cURL from PHP; they are useful when you want to change things like the user-agent string, include a referer header, and when you want to support for cookies when performing HTTP requests. Custom request headers may be defined for all HTTP request types.
after your curl execution use $header_data= curl_getinfo ($curl_exec); This gets the header of the request rather than the header of the response. 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.
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>";
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With