Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse response headers in PHP?

I have made an oauth signed request to a REST API and have the response headers in an array like so:

[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private
[2] => Transfer-Encoding: chunked
[3] => Content-Type: text/html; charset=utf-8
[4] => Content-Location: https://***
[5] => Server: Microsoft-IIS/7.0
[6] => Set-Cookie: ASP.NET_SessionId=***; path=/; HttpOnly
[7] => X-AspNetMvc-Version: 2.0
[8] => oauth_token: ***
[9] => oauth_token_secret: ***
[10] => X-AspNet-Version: 4.0.30319
[11] => X-Powered-By: ASP.NET
[12] => Date: Sat, 15 Sep 2012 02:01:15 GMT

I am trying to figure out how to parse the headers for easy retrieval of items such as the HTTP status code, Content-Location, oauth_token, and oauth_token_secret?

like image 447
pgtips Avatar asked Sep 15 '12 02:09

pgtips


People also ask

How do I view response headers in PHP?

Response Headers in cURLBy setting the CURLOPT_HEADER and CURLOPT_NOBODY options to true, the result of curl_exec() will only contain the headers. This is useful when you only need the headers, but most of the time, we need the content of the request too. The solution to this lies in the CURLOPT_HEADER option as well.

How do I get cURL response header?

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 . The < lines are response headers .

How can I get header in PHP?

PHP | get_headers() Function The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string. It defines the target URL.

What is PHP $Http_response_header?

Introduction. The superglobal $http_response_header array is populated by HTTP response headers as is the case with get_headers() functions. This array is created in local space of PHP.


2 Answers

You'll need to iterate the array and check stripos() to find the header you're looking for. In most cases, you then explode() on : (limiting to 2 resultant parts), but the HTTP response code will require you to explode on the spaces.

// Get any header except the HTTP response...
function getResponseHeader($header, $response) {
  foreach ($response as $key => $r) {
     // Match the header name up to ':', compare lower case
     if (stripos($r, $header . ':') === 0) {
        list($headername, $headervalue) = explode(":", $r, 2);
        return trim($headervalue);
     }
  }
}
// example:
echo getResponseHeader("Content-Type");
// text/html; charset=utf-8

// Get the HTTP response code
foreach ($response as $key => $r) {
  if (stripos($r, 'HTTP/') === 0) {
    list(,$code, $status) = explode(' ', $r, 3);
    echo "Code: $code, Status: $status";
    break;
  }
}
like image 59
Michael Berkowski Avatar answered Oct 12 '22 04:10

Michael Berkowski


I ended up with this solution which uses regex to find all the keys and values in the header combined with some array mutation from https://stackoverflow.com/a/43004994/271351 to get the regex matches into an associative array. This isn't 100% appropriate for the problem asked here since it takes in a string, but joining an array of strings to get a single string would work as a precursor to this. My case had to deal with raw headers, thus this solution.

preg_match_all('/^([^:\n]*): ?(.*)$/m', $header, $headers, PREG_SET_ORDER);
$headers = array_merge(...array_map(function ($set) {
    return array($set[1] => trim($set[2]));
}, $headers));

This yields an associative array of the headers. If the first line of the headers is included as input (e.g. GET / HTTP/1.1), this will ignore it for the output.

like image 29
cjbarth Avatar answered Oct 12 '22 02:10

cjbarth