您的位置:首页 > 其它

判断远程url是否有效

2013-10-07 10:33 218 查看

使用PHP解决

使用file_get_contents函数,不过优缺点如果url无法访问,会出现终止程序问题
使用curl返回,然后判断是否正确执行
使用get_headers函数,根据HTTP返回值查看是否有200

需要配置

      allow_url_fopen=on  修改php.ini,运行使用远程打开

函数介绍

array get_headers ( string $url [, int $format ] )

get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。

如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息。

如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。 例如:

[work@localhost root]$ php -r export(get_headers('http://baidu.com',1));"

array (

  0 => 'HTTP/1.1 200 OK',

  'Date' => 'Mon, 07 Oct 2013 02:07:20 GMT',

  'Server' => 'Apache',

  'Cache-Control' => 'max-age=86400',

  'Expires' => 'Tue, 08 Oct 2013 02:07:20 GMT',

  'Last-Modified' => 'Tue, 12 Jan 2010 13:48:00 GMT',

  'ETag' => '"51-4b4c7d90"',

  'Accept-Ranges' => 'bytes',

  'Content-Length' => '81',

  'Connection' => 'Close',

  'Content-Type' => 'text/html',

)

[work@localhost root]$ php -r "var_export(get_headers('http://baidu.com'));"

array (

  0 => 'HTTP/1.1 200 OK',

  1 => 'Date: Mon, 07 Oct 2013 02:17:34 GMT',

  2 => 'Server: Apache',

  3 => 'Cache-Control: max-age=86400',

  4 => 'Expires: Tue, 08 Oct 2013 02:17:34 GMT',

  5 => 'Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT',

  6 => 'ETag: "51-4b4c7d90"',

  7 => 'Accept-Ranges: bytes',

  8 => 'Content-Length: 81',

  9 => 'Connection: Close',

  10 => 'Content-Type: text/html',

)

自定义get_headers()函数:

if (!function_exists('get_headers')) {   //使用前,最好检测
function get_headers($url, $format=0) {
$headers = array();
$url = parse_url($url);
$host = isset($url['host']) ? $url['host'] : '';
$port = isset($url['port']) ? $url['port'] : 80;
$path = (isset($url['path']) ? $url['path'] : '/') . (isset($url['query']) ? '?' . $url['query'] : '');
$fp = fsockopen($host, $port, $errno, $errstr, 3);
if ($fp)
{
$hdr = "GET $path HTTP/1.1\r\n";
$hdr .= "Host: $host \r\n";
$hdr .= "Connection: Close\r\n\r\n";
fwrite($fp, $hdr);
while (!feof($fp) && $line = trim(fgets($fp, 1024)))
{
if ($line == "\r\n") break;
list($key, $val) = explode(': ', $line, 2);
if ($format)
if ($val) $headers[$key] = $val;
else $headers[] = $key;
else $headers[] = $line;
}
fclose($fp);
return $headers;
}
return false;
}
}


只想获取状态码:

<?php
function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}
?>

页面有重定向怎么办?

$url = 'http://google.com';
var_dump(get_headers($url,0));
array(18) {
[0]=>  string(30) "HTTP/1.0 301 Moved Permanently"
[1]=>  string(32) "Location: http://www.google.com/" [2]=>  string(38) "Content-Type: text/html; charset=UTF-8"
[3]=>  string(35) "Date: Sun, 26 Sep 2010 00:59:50 GMT"
[4]=>  string(38) "Expires: Tue, 26 Oct 2010 00:59:50 GMT"
[5]=>  string(38) "Cache-Control: public, max-age=2592000"
....
string(15) "HTTP/1.0 200 OK"
[10]=>  string(35) "Date: Sun, 26 Sep 2010 00:59:51 GMT"
[11]=>  string(11) "Expires: -1"
[12]=>  string(33) "Cache-Control: private, max-age=0"
.....
}

/*===========================*/

var_dump(get_headers($url,1));
array(11) {
[0]=>
string(30) "HTTP/1.0 301 Moved Permanently"
["Location"]=>  string(22) "http://www.google.com/"
["Content-Type"]=>  array(2) {
[0]=>    string(24) "text/html; charset=UTF-8"
[1]=>    string(29) "text/html; charset=ISO-8859-1"
}
["Date"]=>  array(2) {
[0]=>    string(29) "Sun, 26 Sep 2010 01:03:39 GMT"
[1]=>    string(29) "Sun, 26 Sep 2010 01:03:39 GMT"
}
["Expires"]=>  array(2) {
[0]=>    string(29) "Tue, 26 Oct 2010 01:03:39 GMT"
[1]=>    string(2) "-1"
}
["Cache-Control"]=>  array(2) {
[0]=>    string(23) "public, max-age=2592000"
[1]=>    string(18) "private, max-age=0"
}
.....
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: