HTTPヘッダの取得

現在のページへのリクエストから取得

array getallheaders( void )
PHP: getallheaders - Manual

実行例

$headers = getallheaders();
Array
(
    [Accept] => text/html, application/xhtml+xml, */*
    [Accept-Language] => ja-JP
    [User-Agent] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
    [UA-CPU] => AMD64
    [Accept-Encoding] => gzip, deflate
    [Host] => localhost
    [Connection] => Keep-Alive
)

指定ページへのリクエストから取得

get_headers()を使用する方法

array get_headers( string $url [, int $format = 0 ] )
PHP: get_headers - Manual
$headers = get_headers($url, 1);

$location = array_key_exists('Location', $headers) ? $headers['Location'] : '';
echo implode("\t", array($url, $headers[0], $location)) . "\n";

実行例

$headers = get_headers( 'http://www.yahoo.co.jp' );
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Fri, 08 Jul 2011 15:06:53 GMT
    [2] => P3P: policyref="http://privacy.yahoo.co.jp/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
    [3] => Expires: -1
    [4] => Pragma: no-cache
    [5] => Cache-Control: no-cache, private
    [6] => Cache-Control: private, no-store, must-revalidate
    [7] => X-XRDS-Location: http://open.login.yahoo.co.jp/openid20/www.yahoo.co.jp/xrds
    [8] => Vary: Accept-Encoding
    [9] => Connection: close
    [10] => Content-Type: text/html; charset=utf-8
)
formatパラメータを指定した場合
$headers = get_headers( 'http://www.yahoo.co.jp', true );
Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Fri, 08 Jul 2011 15:07:49 GMT
    [P3P] => policyref="http://privacy.yahoo.co.jp/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
    [Expires] => -1
    [Pragma] => no-cache
    [Cache-Control] => Array
        (
            [0] => no-cache, private
            [1] => private, no-store, must-revalidate
        )

    [X-XRDS-Location] => http://open.login.yahoo.co.jp/openid20/www.yahoo.co.jp/xrds
    [Vary] => Accept-Encoding
    [Connection] => close
    [Content-Type] => text/html; charset=utf-8
)

fsockopen()を使用する方法

resource fsockopen(
    string $hostname   // ホスト名
    [, int $port = -1  // ポート番号
    [, int &$errno     // エラー番号
    [, string &$errstr // エラーメッセージ
    [, float $timeout = ini_get( "default_socket_timeout" ) // 接続タイムアウト秒数
    ]]]] )
PHP: fsockopen - Manual

サンプルコード

$url = parse_url( 'http://www.yahoo.co.jp' );
$filePointer = fsockopen( $url[ 'host' ], 80 );

if( $filePointer )
{
    @$requestUrl = $url[ 'path' ].'?'.$url[ 'query' ];

    $requestMessage =
        "HEAD {$requestUrl} HTTP/1.1\r\n".
        "Host: {$url[ 'host' ]}\r\n".
        "\r\n";

    if( fwrite( $filePointer, $requestMessage ) )
    {
        while( !feof( $filePointer ) )
        {
            echo fgets( $filePointer );
        }
    }

    fclose( $filePointer );
}

取得例

HTTP/1.1 200 OK
Date: Wed, 24 Jun 2009 11:32:14 GMT
Last-Modified: Wed, 24 Jun 2009 11:32:14 GMT
Accept-Ranges: bytes
Expires: Tue, 23 Jun 2009 11:32:14 GMT
Pragma: no-cache
Content-Length: 4855
Cache-Control: private
Connection: close
Content-Type: text/html
PHPのマニュアルから検索