url是否可以正常访问,是否是url

xiaoxiao2021-02-28  69

一个链接是否可以正常访问:

方法一:

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() 将返回 FALSEfile_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

方法四:

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

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://www.6miu.com/read-2620731.html

最新回复(0)