Share via:

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.

4155 views

Edited: 2018-02-03 17:32

cURL headers, and server responses.

When a HTTP request has been received by a server, typically a response is sent back to the client. In the case of web resources, the response will usually consist of two parts, the Response Header and the Response Body.

The header part of the response contains all the response headers, including cookies (if any) and information about the mime type of the content. The response body contains the content, which should match the mine-type in the headers. For example, common mime-types might be text/html for HTML pages, and text/css for external StyleSheets.

To show the response headers to a given request, we can simply cut out the headers using a combination of the options and functions. A more detailed explanation of this is available in the next section of the article, for those interested.

A quick full example is included below:

$url = "https://beamtic.com/";

$ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, 1);

  $response = curl_exec($ch);
  
  // Retudn headers seperatly from the Response Body
  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $headers = substr($response, 0, $header_size);
  $body = substr($response, $header_size);
  
curl_close($ch);

header("Content-Type:text/plain; charset=UTF-8");
echo $headers;
echo $body;

The above will output something like:

HTTP/1.1 200 OK
Date: Fri, 02 Feb 2018 17:33:11 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: must-revalidate
Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT
Expires: -1
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

Retrieving the response headers

There is no build-in way to only return the response headers using cURL in PHP. However, we can still "cut" them from the full response. To do this, we first determine the size of the response header, and then simply cut it from the response using the substr() function.

First, we set the CURLOPT_HEADER option true. Doing this will include the headers in the response downloaded by cURL. Next, we will need to cut out the headers.

Using this method, we can return both the body and header part of the response. However, before we can separate the components of the response, we need to get the size of the header. This can be done using curl_getinfo() with the CURLINFO_HEADER_SIZE option, as shown below:

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

Finally, we may use the value returned by curl_getinfo() with substr() to seperate the headers from the response message.

$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);

Create an array containing each header

In order to better work with the individual headers, we should place them in an array. HTTP Headers are separated by a Carriage Return and a Line Feed, also known as CRLF (Sometimes also represented as: [CR][LF]). Knowing this, we can create an array from the raw headers using the PHP explode() function.

$headers_arr = explode("\r\n", $headers); // The seperator used in the Response Header is CRLF (Aka. \r\n)
print_r($headers_arr); // Shows the content of the $headers_arr array

As you can see, we also have a couple of empty enteries. To remove those, we can pass the array to the array_filter() function:

$headers_arr = array_filter($headers_arr);

Finally, we can easily work on the array in a loop.

$html = '';
foreach ($headers as &$value) {
    $html .= '<li>' . $value . '</li>';
}
$html = '<ol>' . $html . '</ol>';

Finished example:

// URL to fetch
$url = "https://beamtic.com/";

$ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, 1);

  $response = curl_exec($ch);
  
  // Retudn headers seperatly from the Response Body
  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $headers = substr($response, 0, $header_size);
  $body = substr($response, $header_size);
  
curl_close($ch);

$headers = explode("\r\n", $headers); // The seperator used in the Response Header is CRLF (Aka. \r\n) 

$headers = array_filter($headers);

$html = '';
foreach ($headers as &$value) {
    $html .= '<li>' . $value . '</li>';
}
$html = '<ol>' . $html . '</ol>';

header("Content-Type:text/html; charset=UTF-8");
echo $html;

Comments