目录
一个链接是否可以正常访问:方法一:array get_headers( string $url[, int $format = 0] )
url: 目标 URL。
format: 如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
返回:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
方法二:fopen — 打开文件或者 URL
resource fopen( string $filename, string $mode[, bool $use_include_path = false[, resource $context]] )
fopen() 将 filename 指定的名字资源绑定到一个流上。
成功时返回文件指针资源,如果打开失败,本函数返回 FALSE。
如果打开失败,会产生一个 E_WARNING 错误。可以通过 @ 来屏蔽错误。
‘r’ 只读方式打开,将文件指针指向文件头。
function varify_url($url)
{
$check = @fopen($url,"r");
if($check)
$status = true;
else
$status = false;
return $status;
}
方法三:curl检测
public function httpcode($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_URL, $url);
//忽略证书,不然的话https会返回0
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
url检查这种方式比较多人使用
补充:检测是否是有效的url
public function validateActiveUrl($value)
{
if (! is_string($value)) {
return false;
}
if ($url = parse_url($value, PHP_URL_HOST)) {
try {
return count(dns_get_record($url, DNS_A | DNS_AAAA)) > 0;
} catch (Exception $e) {
return false;
}
}
return false;
}
原文:https://blog.csdn.net/zhezhebie/article/details/80361115
历史上的今天
© 版权声明