PHPで取得する方法めも。
$url = 'http://example.com'; $res = get_headers($url); var_dump($res); ↓ array(13) { [0]=> string(15) "HTTP/1.0 200 OK" [1]=> string(20) "Accept-Ranges: bytes" [2]=> string(29) "Cache-Control: max-age=604800" [3]=> string(23) "Content-Type: text/html" [4]=> string(35) "Date: Sun, 08 Sep 2013 15:49:42 GMT" [5]=> string(18) "Etag: "3012602696"" [6]=> string(38) "Expires: Sun, 15 Sep 2013 15:49:42 GMT" [7]=> string(44) "Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT" [8]=> string(22) "Server: ECS (sjc/4FCE)" [9]=> string(12) "X-Cache: HIT" [10]=> string(20) "x-ec-custom-error: 1" [11]=> string(20) "Content-Length: 1270" [12]=> string(17) "Connection: close" }
get_headers()の第2引数に0以外の値を渡すと、配列のキーに「Content-Type」やら「Content-Length」やらが入ってくれる。
$url = 'http://example.com'; $res = get_headers($url, 1); var_dump($res); ↓ array(13) { [0]=> string(15) "HTTP/1.0 200 OK" ["Accept-Ranges"]=> string(5) "bytes" ["Cache-Control"]=> string(14) "max-age=604800" ["Content-Type"]=> string(9) "text/html" ["Date"]=> string(29) "Sun, 08 Sep 2013 15:52:38 GMT" ["Etag"]=> string(12) ""3012602696"" ["Expires"]=> string(29) "Sun, 15 Sep 2013 15:52:38 GMT" ["Last-Modified"]=> string(29) "Fri, 09 Aug 2013 23:54:35 GMT" ["Server"]=> string(14) "ECS (sjc/4FB4)" ["X-Cache"]=> string(3) "HIT" ["x-ec-custom-error"]=> string(1) "1" ["Content-Length"]=> string(4) "1270" ["Connection"]=> string(5) "close" }
関数の定義は以下の通り。
array get_headers(string $url [, int $format = 0 ])
どこかで使えそうだったので一応メモ。