如何根据url是绝对的还是相对的来检查字符串是否有服务器?
function hasServer($url){
...
}
如果是,则返回真正的
hasServer('http://www.google.com/');
否则返回错误
hasServer('/about-us/team/');
我想这就是您想要的:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
根据我的经验,实际上有几个选项。一种方法是使用headers()
函数,然后分析在生成的数组中获得的信息。
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
输出应该为您提供有关url是否真的存在的所有信息。
在我看来,一个更简单的解决方案是检查fopen()
函数是否真的对url有效!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
您可以选择您更需要的服务器。