PHP检测URL是否存在
本文分享通过PHP检测给定的URL是否存在的小脚本,代码如下:
<?php
function url_exists($url) {
$hdrs = @get_headers($url);
return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;
}
$result = url_exists("http://phpcode8.com/phpscripts/wordpress-is-plugin-active.html");
if($result){
echo "PHP检测到URL存在!";
}else{
echo "PHP检测该URL不存在";
}
?>
将以上代码保存为test.php即可测试结果。
浙公网安备 33010602011771号