PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

http_parse_message> <http_parse_cookie
Last updated: Fri, 05 Sep 2008

view this page in

http_parse_headers

(PECL pecl_http:0.10.0-1.5.5)

http_parse_headersHTTP ヘッダをパースする

説明

array http_parse_headers ( string $header )

HTTP ヘッダをパースし、連想配列に格納します。

パラメータ

header

HTTP ヘッダを含む文字列。

返り値

成功した場合に配列、失敗した場合に FALSE を返します。

例1 http_parse_headers() の使用法

<?php
$headers 
"content-type: text/html; charset=UTF-8\r\n".
  
"Server: Funky/1.0\r\n".
  
"Set-Cookie: foo=bar\r\n".
  
"Set-Cookie: baz=quux\r\n".
  
"Folded: works\r\n\ttoo\r\n";
print_r(http_parse_headers($headers));
?>

上の例の出力は以下となります。

Array
(
  [Content-Type] => text/html; chatset=UTF-8
  [Server] => Funky/1.0
  [Set-Cookie] => Array
  (
    [0] => foo=bar
    [1] => baz=quux
  )
  [Folded] => works
    too
)



add a note add a note User Contributed Notes
http_parse_headers
user at example dot net
21-Aug-2007 01:45
This one works better:

<?php

   
function http_parse_headers( $header )
    {
       
$retVal = array();
       
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach(
$fields as $field ) {
            if(
preg_match('/([^:]+): (.+)/m', $field, $match) ) {
               
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                if( isset(
$retVal[$match[1]]) ) {
                   
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                } else {
                   
$retVal[$match[1]] = trim($match[2]);
                }
            }
        }
        return
$retVal;
    }

?>
luigi dot sexpistols at gmail dot com
08-Aug-2006 07:49
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"
}
?>

http_parse_message> <http_parse_cookie
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites