Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : HTTP : http_parse_headers

http_parse_headers

Parse HTTP headers (PECL pecl_http:0.10.0-1.5.5)
array http_parse_headers ( string header )

Example 890. Using 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));
?>

The above example will output:

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

Code Examples / Notes » http_parse_headers

user

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;
}
?>


web

small addition for
user at example dot net
<?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]]) ) {
                   if (is_array($retVal[$match[1]]))
                   {
                       array_push($retVal[$match[1]], $match[2]);
                   }
                   else
                   {
                       $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                   }
               } else {
                   $retVal[$match[1]] = trim($match[2]);
               }
           }
       }
       return $retVal;
   }
?>


luigi dot sexpistols

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"
}
?>


Change Language


Follow Navioo On Twitter
Installation
Configuration
Resources
Constants
Request Options
HttpMessage
HttpMessage::__construct
HttpMessage::factory
HttpMessage::fromEnv
HttpMessage::fromString
HttpMessage::toString
HttpMessage::toMessageTypeObject
HttpMessage::guessContentType
HttpMessage::detach
HttpMessage::prepend
HttpMessage::reverse
HttpMessage::send
HttpMessage::getParentMessage
HttpMessage::getType
HttpMessage::setType
HttpMessage::getHttpVersion
HttpMessage::setHttpVersion
HttpMessage::getHeaders
HttpMessage::getHeader
HttpMessage::addHeaders
HttpMessage::setHeaders
HttpMessage::getBody
HttpMessage::setBody
HttpMessage::getRequestMethod
HttpMessage::setRequestMethod
HttpMessage::getRequestUrl
HttpMessage::setRequestUrl
HttpMessage::getResponseCode
HttpMessage::setResponseCode
HttpMessage::getResponseStatus
HttpMessage::setResponseStatus
HttpQueryString
HttpQueryString::__construct
HttpQueryString::singleton
HttpQueryString::get
HttpQueryString::mod
HttpQueryString::set
HttpQueryString::toArray
HttpQueryString::toString
HttpQueryString::xlate
HttpDeflateStream
HttpDeflateStream::factory
HttpDeflateStream::__construct
HttpDeflateStream::update
HttpDeflateStream::flush
HttpDeflateStream::finish
HttpInflateStream
HttpInflateStream::factory
HttpInflateStream::__construct
HttpInflateStream::update
HttpInflateStream::flush
HttpInflateStream::finish
HttpRequest
HttpRequest::addCookies
HttpRequest::addHeaders
HttpRequest::addPostFields
HttpRequest::addPostFile
HttpRequest::addPutData
HttpRequest::addQueryData
HttpRequest::addRawPostData
HttpRequest::addSslOptions
HttpRequest::clearHistory
HttpRequest::__construct
HttpRequest::enableCookies
HttpRequest::getContentType
HttpRequest::getCookies
HttpRequest::getHeaders
HttpRequest::getHistory
HttpRequest::getMethod
HttpRequest::getOptions
HttpRequest::getPostFields
HttpRequest::getPostFiles
HttpRequest::getPutData
HttpRequest::getPutFile
HttpRequest::getQueryData
HttpRequest::getRawPostData
HttpRequest::getRawRequestMessage
HttpRequest::getRawResponseMessage
HttpRequest::getRequestMessage
HttpRequest::getResponseBody
HttpRequest::getResponseCode
HttpRequest::getResponseCookies
HttpRequest::getResponseData
HttpRequest::getResponseHeader
HttpRequest::getResponseInfo
HttpRequest::getResponseMessage
HttpRequest::getResponseStatus
HttpRequest::getSslOptions
HttpRequest::getUrl
HttpRequest::resetCookies
HttpRequest::send
HttpRequest::setContentType
HttpRequest::setCookies
HttpRequest::setHeaders
HttpRequest::setMethod
HttpRequest::setOptions
HttpRequest::setPostFields
HttpRequest::setPostFiles
HttpRequest::setPutData
HttpRequest::setPutFile
HttpRequest::setQueryData
HttpRequest::setRawPostData
HttpRequest::setSslOptions
HttpRequest::setUrl
HttpRequestPool
HttpRequestPool::attach
HttpRequestPool::__construct
HttpRequestPool::__destruct
HttpRequestPool::detach
HttpRequestPool::getAttachedRequests
HttpRequestPool::getFinishedRequests
HttpRequestPool::reset
HttpRequestPool::send
HttpRequestPool::socketPerform
HttpRequestPool::socketSelect
HttpResponse
HttpResponse::capture
HttpResponse::getBufferSize
HttpResponse::getCacheControl
HttpResponse::getCache
HttpResponse::getContentDisposition
HttpResponse::getContentType
HttpResponse::getData
HttpResponse::getETag
HttpResponse::getFile
HttpResponse::getGzip
HttpResponse::getHeader
HttpResponse::getLastModified
HttpResponse::getStream
HttpResponse::getThrottleDelay
HttpResponse::getRequestBody
HttpResponse::getRequestBodyStream
HttpResponse::getRequestHeaders
HttpResponse::guessContentType
HttpResponse::redirect
HttpResponse::send
HttpResponse::setBufferSize
HttpResponse::setCacheControl
HttpResponse::setCache
HttpResponse::setContentDisposition
HttpResponse::setContentType
HttpResponse::setData
HttpResponse::setETag
HttpResponse::setFile
HttpResponse::setGzip
HttpResponse::setHeader
HttpResponse::setLastModified
HttpResponse::setStream
HttpResponse::setThrottleDelay
HttpResponse::status
http_cache_etag
http_cache_last_modified
http_chunked_decode
http_deflate
http_inflate
http_get_request_body_stream
http_get_request_body
http_get_request_headers
http_date
http_support
http_match_etag
http_match_modified
http_match_request_header
http_build_cookie
http_negotiate_charset
http_negotiate_content_type
http_negotiate_language
ob_deflatehandler
ob_etaghandler
ob_inflatehandler
http_parse_cookie
http_parse_headers
http_parse_message
http_parse_params
http_persistent_handles_count
http_persistent_handles_ident
http_persistent_handles_clean
http_get
http_head
http_post_data
http_post_fields
http_put_data
http_put_file
http_put_stream
http_request_method_exists
http_request_method_name
http_request_method_register
http_request_method_unregister
http_request
http_request_body_encode
http_redirect
http_send_content_disposition
http_send_content_type
http_send_data
http_send_file
http_send_last_modified
http_send_status
http_send_stream
http_throttle
http_build_str
http_build_url
eXTReMe Tracker