如何检查PHP中是否存在URL (不是404)?
发布于 2010-02-17 20:10:32
这里:
$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;
}
在上面的帖子中,来自here和right below,有一个curl解决方案:
function url_exists($url) {
return curl_init($url) !== false;
}
发布于 2012-04-04 22:55:26
$headers = @get_headers($this->_value);
if(strpos($headers[0],'200')===false)return false;
所以,任何时候你联系一个网站,得到比200ok更多的东西,它都会工作。
发布于 2013-07-24 19:03:00
你不能在某些服务器上使用curl,你可以使用下面的代码
<?php
$url = 'http://www.example.com';
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200"))
{
echo 'url exists';
}
else
{
echo 'url does not exist';
}
?>
https://stackoverflow.com/questions/2280394
复制相似问题