note 68698 added to function.http-parse-headers
| From: | luigi dot sexpistols at gmail dot com | Date: | Tue, 08 Aug 2006 05:49:47 +0000 |
| Subject: | note 68698 added to function.http-parse-headers | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115619@lists.php.net to get a copy of this message | ||
If you don't have access to the PECL library, you can use this code to parse headers contained
in strings.
Note that it's probably not as robust as the PECL version, as it only parses if the headers are
separated by newlines (\n). This isn't a problem in most cases, though, as the standard
suggests to use \r\n as the delimiter for headers.
HTTP response code is put into 'status'.
Any suggestions welcome!
<?
function http_parse_headers($headers=false){
if($headers === false){
return false;
}
$headers = str_replace("\r","",$headers);
$headers = explode("\n",$headers);
foreach($headers as $value){
$header = explode(": ",$value);
if($header[0] && !$header[1]){
$headerdata['status'] = $header[0];
}
elseif($header[0] && $header[1]){
$headerdata[$header[0]] = $header[1];
}
}
return $headerdata;
}
$headers = "HTTP/1.1 200 OK\r\n
Date: Tue, 08 Aug 2006 05:32:01 GMT\r\n
X-Powered-By: PHP/4.4.3-dev\r\n
Data 1: Value for Data 1\r\n
Data 2: Value for Data 2\r\n
Connection: close\r\n
Content-Type: text/html\r\n";
http_parse_headers($headers);
// OUTPUT:
array(7) {
["status"]=>
string(15) "HTTP/1.1 200 OK"
["Date"]=>
string(29) "Tue, 08 Aug 2006 05:32:01 GMT"
["X-Powered-By"]=>
string(13) "PHP/4.4.3-dev"
["Data 1"]=>
string(16) "Value for Data 1"
["Data 2"]=>
string(16) "Value for Data 2"
["Connection"]=>
string(5) "close"
["Content-Type"]=>
string(9) "text/html"
}
?>
----
Server IP: 203.16.234.20
Probable Submitter: 203.213.7.131 (proxied: 60.242.13.79)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.http-parse-headers.php
Edit -- https://master.php.net/note/edit/68698
Del: integrated -- https://master.php.net/note/delete/68698/integrated
Del: useless -- https://master.php.net/note/delete/68698/useless
Del: bad code -- https://master.php.net/note/delete/68698/bad+code
Del: spam -- https://master.php.net/note/delete/68698/spam
Del: non-english -- https://master.php.net/note/delete/68698/non-english
Del: in docs -- https://master.php.net/note/delete/68698/in+docs
Del: other reasons-- https://master.php.net/note/delete/68698
Reject -- https://master.php.net/note/reject/68698
Search -- https://master.php.net/manage/user-notes.php