http_parse_message
(PECL pecl_http >= 0.12.0)
http_parse_message — Parse HTTP messages
Description
object http_parse_message
( string
$message
)
Parses the HTTP message
into a simple
recursive object.
Parameters
-
message
-
string containing a single HTTP message or several consecutive HTTP messages
Return Values
Returns a hierarchical object structure of the parsed messages.
Examples
Example #1 Using http_parse_message()
<?php
define ('URL', 'http://www.example.com/');
print_r(http_parse_message(http_get(URL, array('redirect' => 3))));
?>
The above example will output something similar to:
stdClass object ( [type] => 2 [httpVersion] => 1.1 [responseCode] => 200 [headers] => Array ( [Content-Length] => 3 [Server] => Apache ) [body] => Hi! [parentMessage] => stdClass object ( [type] => 2 [httpVersion] => 1.1 [responseCode] => 302 [headers] => Array ( [Content-Length] => 0 [Location] => ... ) [body] => [parentMessage] => ... ) )
[an error occurred while processing the directive]
- http_cache_etag
- http_cache_last_modified
- http_chunked_decode
- http_deflate
- http_inflate
- http_build_cookie
- http_date
- http_get_request_body_stream
- http_get_request_body
- http_get_request_headers
- http_match_etag
- http_match_modified
- http_match_request_header
- http_support
- 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_clean
- http_persistent_handles_count
- http_persistent_handles_ident
- http_get
- http_head
- http_post_data
- http_post_fields
- http_put_data
- http_put_file
- http_put_stream
- http_request_body_encode
- http_request_method_exists
- http_request_method_name
- http_request_method_register
- http_request_method_unregister
- http_request
- 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
Коментарии
A nice PHP alternative, might need work, but works for me and can be an entry point for someone else. It doesn't do exactly the same as this function, but it rather parses all headers and extracts the HTTP and Content response on it's own array element.
<?php
function http_parse_response($header) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach ($fields as $field) {
// Do not process empty cubrid_num_fields(result)
if (empty($field)) {
continue;
}
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]])) {
$retVal[$match[1]] = array($retVal[$match[1]]);
}
$retVal[$match[1]][] = $match[2];
}
else {
$retVal[$match[1]] = trim($match[2]);
}
}
else {
if (preg_match('/HTTP\//', $field)) {
// Following HTTP standards which are space-separated
preg_match('/(.*?) (.*?) (.*)/', $field, $matches);
$retVal['HTTP']['version'] = $matches[1];
$retVal['HTTP']['code'] = $matches[2];
$retVal['HTTP']['reason'] = $matches[3];
}
else {
$retVal['Content'][] = $field;
}
}
}
return $retVal;
}
?>