php检测url是否存在

function file_exists($url) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
//不下载
curl_setopt($ch, CURLOPT_NOBODY, 1);
//设置超时
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code == 200 || $http_code == 301) {
return true;
}
return false;
}
posted @ 2021-08-04 17:51  飞鹰之歌  阅读(83)  评论(0)    收藏  举报