一個連結是否可以正常通路:
方法一:
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' 隻讀方式打開,将檔案指針指向檔案頭。
方法三:
和 file() 一樣,隻除了 file_get_contents() 把檔案讀入一個字元串。将在參數 offset 所指定的位置開始讀取長度為 maxlen 的内容。如果失敗,file_get_contents() 将傳回 FALSE。 file_get_contents() 函數是用來将檔案的内容讀入到一個字元串中的首選方法。如果作業系統支援還會使用記憶體映射技術來增強性能。
file_get_contents() 函數是用來将檔案的内容讀入到一個字元串中的首選方法。如果作業系統支援還會使用記憶體映射技術來增強性能。
方法四:
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;
}
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;
}