HttpMessage
在线手册:中文 英文
PHP手册

HttpMessage::getHeader

(PECL pecl_http >= 1.1.0)

HttpMessage::getHeaderGet header

说明

public string HttpMessage::getHeader ( string $header )

Get message header.

参数

header

header name

返回值

Returns the header value on success or NULL if the header does not exist.


HttpMessage
在线手册:中文 英文
PHP手册
PHP手册 - N: Get header

用户评论:

thiago dot henrique dot mata at gmail dot com (19-Mar-2011 08:31)

This can be good to ping external web sites, get the content type, length, etc.
<?php

function get_http_header( $strUrl )
{
   
$arrHeader = array();
   
$arrHeader[ 'url' ] = $strUrl;
    try
    {
       
$arrLines = explode( "\n" , http_head( $strUrl ) );
    }
    catch(
Exception $objException )
    {
        return
    array(
       
"response" => "timeout" ,
       
"url" => $strUrl
   
);
    }
   
$arrHeader['response'] = array_shift( $arrLines );
    foreach(
$arrLines as $strLine )
    {
       
$arrLine = explode( ":" , $strLine );
        if(
sizeof( $arrLine ) == 2 )
        {
           
$arrHeader[ $arrLine[0] ] = $arrLine[1];
        }   
    }
    return
$arrHeader;
}

print_r( get_http_header( "http://www.example.com" ) );
?>

Array
(
    [url] => http://www.example.com
    [response] => HTTP/1.1 200 OK
    [Server] =>  Microsoft-IIS/5.0
    [X-Powered-By] =>  ASP.NET
    [Content-Type] =>  text/html
    [Accept-Ranges] =>  bytes
    [Content-Length] =>  465