我如何检查一个URL是否存在(不是404)在PHP?
当前回答
我运行一些测试,看看我的网站上的链接是否有效-提醒我当第三方改变他们的链接。我有一个网站的问题,有一个配置不良的证书,这意味着php的get_headers不能工作。
所以,我读到卷曲更快,并决定给一个尝试。然后我在领英上遇到了一个问题,给了我一个999错误,后来证明是用户代理的问题。
我不关心证书是否对该测试无效,也不关心响应是否为重定向。
然后我认为使用get_headers无论如何,如果卷曲失败....
试试看....
/**
* returns true/false if the $url is valid.
*
* @param string $url assumes this is a valid url.
*
* @return bool
*/
private function urlExists(string $url): bool
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // do not output response in stdout
curl_setopt($ch, CURLOPT_NOBODY, true); // this does a head request to make it faster.
curl_setopt($ch, CURLOPT_HEADER, true); // just the headers
curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, false); // turn off that pesky ssl stuff - some sys admins can't get it right.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// set a real user agent to stop linkedin getting upset.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($http_code >= 200 && $http_code < 400) || $http_code === 999) {
curl_close($ch);
return true;
}
//$error = curl_error($ch); // used for debugging.
curl_close($ch);
// just try the get_headers - it might work!
stream_context_set_default(
['http' => ['method' => 'HEAD']]
);
$file_headers = @get_headers($url);
if ($file_headers !== false) {
$response_code = substr($file_headers[0], 9, 3);
return $response_code >= 200 && $response_code < 400;
}
return false;
}
其他回答
function urlIsOk($url)
{
$headers = @get_headers($url);
$httpStatus = intval(substr($headers[0], 9, 3));
if ($httpStatus<400)
{
return true;
}
return false;
}
在这里:
$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
从这里和上面帖子的正下方,有一个卷曲的解决方案:
function url_exists($url) {
return curl_init($url) !== false;
}
我使用这个函数:
/**
* @param $url
* @param array $options
* @return string
* @throws Exception
*/
function checkURL($url, array $options = array()) {
if (empty($url)) {
throw new Exception('URL is empty');
}
// list of HTTP status codes
$httpStatusCodes = array(
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
511 => 'Network Authentication Required',
599 => 'Network Connect Timeout Error'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if (isset($options['timeout'])) {
$timeout = (int) $options['timeout'];
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
}
curl_exec($ch);
$returnedStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (array_key_exists($returnedStatusCode, $httpStatusCodes)) {
return "URL: '{$url}' - Error code: {$returnedStatusCode} - Definition: {$httpStatusCodes[$returnedStatusCode]}";
} else {
return "'{$url}' does not exist";
}
}
cURL可以返回HTTP代码,我不认为所有额外的代码是必要的?
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
我运行一些测试,看看我的网站上的链接是否有效-提醒我当第三方改变他们的链接。我有一个网站的问题,有一个配置不良的证书,这意味着php的get_headers不能工作。
所以,我读到卷曲更快,并决定给一个尝试。然后我在领英上遇到了一个问题,给了我一个999错误,后来证明是用户代理的问题。
我不关心证书是否对该测试无效,也不关心响应是否为重定向。
然后我认为使用get_headers无论如何,如果卷曲失败....
试试看....
/**
* returns true/false if the $url is valid.
*
* @param string $url assumes this is a valid url.
*
* @return bool
*/
private function urlExists(string $url): bool
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // do not output response in stdout
curl_setopt($ch, CURLOPT_NOBODY, true); // this does a head request to make it faster.
curl_setopt($ch, CURLOPT_HEADER, true); // just the headers
curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, false); // turn off that pesky ssl stuff - some sys admins can't get it right.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// set a real user agent to stop linkedin getting upset.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($http_code >= 200 && $http_code < 400) || $http_code === 999) {
curl_close($ch);
return true;
}
//$error = curl_error($ch); // used for debugging.
curl_close($ch);
// just try the get_headers - it might work!
stream_context_set_default(
['http' => ['method' => 'HEAD']]
);
$file_headers = @get_headers($url);
if ($file_headers !== false) {
$response_code = substr($file_headers[0], 9, 3);
return $response_code >= 200 && $response_code < 400;
}
return false;
}
推荐文章
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符
- 如何从URL获取参数与JSP
- 在PHP5中创建单例设计模式
- URL从Java中的类路径加载资源
- 阻止人们入侵基于php的Flash游戏高分表的最佳方法是什么
- 单击url会打开默认浏览器
- PHP子字符串提取。获取第一个'/'之前的字符串或整个字符串
- __construct函数的作用是什么?
- PHP中的异步shell执行器
- Laravel 5 -如何访问在视图存储上传的图像?
- 我如何得到一个Flask请求的url的不同部分?