Reputation: 8891
I am using CURL to execute HTTP requests. I get back something like this:
HTTP/1.1 200 OK
Date: Thu, 07 Apr 2011 15:52:33 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
My question is, how can I reliably retrieve any of the four named fields' values?
Right now I am doing this:
$name = 'Expires';
preg_match('/^'.$name.': ([^\r\n]*)[\r\n]*$/m', $headers, $matches);
It seems to be working. I'm just curious of my regular expression here is going to work for every well-formed header I ever receive from a server, or if there are variances where it would break. I can't seem to find specific restrictions on valid characters, newlines or carriage returns so I don't know if all values will follow the format:
Fieldname followed by colon, followed by space, followed by non-newline characters, followed by newline characters.
Upvotes: 2
Views: 3849
Reputation: 5523
I use this code and don't have problems so far (far from ideal of course):
// $this->headers contains string with all headers
private function parseHeaders()
{
$headers = array();
foreach (explode("\n", $this->headers) as $line) {
$line = trim($line);
if (strpos($line, ':') !== false) {
list($headerName, $headerValue) = explode(':', $line, 2);
$headerValue = ltrim($headerValue);
$headerName = strtolower(rtrim($headerName));
if (isset($headers[$headerName])) {
if (is_array($headers[$headerName])) {
$headers[$headerName][] = $headerValue;
} else {
$headers[$headerName] = array(
$headers[$headerName],
$headerValue
);
}
} else {
$headers[$headerName] = $headerValue;
}
}
}
return $headers;
}
However, if you want to do it in most correct way, I think you should read RFC 2616
Upvotes: 1
Reputation: 24506
PHP has its built-in way of doing this with http_parse_headers(), which has got to be a better way to go than rolling your own.
http://php.net/manual/en/function.http-parse-headers.php
Upvotes: 1
Reputation: 7103
I do not think there is a way it could break. Although, if you know that the order is always the same, then there is no need to include the variable name in the regexp, just start with a semicolon. Then, you could just simplify it to
/:(.*)$/m
the $
should stop right at the end of the line, and (.*)
will cause that you will have the variable value included in the specified group. As you know there should be 4 groups, in the case above.
Upvotes: 2