<?php $url = 'http://www.jb51.net'; print_r(get_headers($url)); print_r(get_headers($url, 1)); ?>
Array ( [0] => HTTP/1.1 200 OK [1] => Cache-Control: max-age=1800 [2] => Content-Length: 54874 [3] => Content-Type: text/html [4] => Content-Location: http://www.jb51.net/index.htm [5] => Last-Modified: Fri, 28 Nov 2014 03:34:56 GMT [6] => Accept-Ranges: bytes [7] => ETag: "b66ba847bcad01:bc5" [8] => Server: Microsoft-IIS/6.0 [9] => Date: Fri, 28 Nov 2014 03:37:34 GMT [10] => Connection: close ) Array ( [0] => HTTP/1.1 200 OK [Cache-Control] => max-age=1800 [Content-Length] => 54874 [Content-Type] => text/html [Content-Location] => http://www.jb51.net/index.htm [Last-Modified] => Fri, 28 Nov 2014 03:34:56 GMT [Accept-Ranges] => bytes [ETag] => "b66ba847bcad01:bc5" [Server] => Microsoft-IIS/6.0 [Date] => Fri, 28 Nov 2014 03:37:35 GMT [Connection] => close )
//判断远程文件是否存在 function remote_file_exists($url) { $executeTime = ini_get('max_execution_time'); ini_set('max_execution_time', 0); $headers = @get_headers($url); ini_set('max_execution_time', $executeTime); if ($headers) { $head = explode(' ', $headers[0]); if ( !emptyempty($head[1]) && intval($head[1]) < 400) return true; } return false; }
<?php /** * Fetches all the real headers sent by the server in response to a HTTP request without redirects * 获取不包含重定向的报头 */ function get_real_headers($url,$format=0,$follow_redirect=0) { if (!$follow_redirect) { //set new default options $opts = array('http' => array('max_redirects'=>1,'ignore_errors'=>1) ); stream_context_get_default($opts); } //get headers $headers=get_headers($url,$format); //restore default options if (isset($opts)) { $opts = array('http' => array('max_redirects'=>20,'ignore_errors'=>0) ); stream_context_get_default($opts); } //return return $headers; } ?>
希望本文所述对大家的PHP程序设计有所帮助。