Heim  >  Artikel  >  Backend-Entwicklung  >  curl判断网址是否存在

curl判断网址是否存在

WBOY
WBOYOriginal
2016-06-06 20:16:171592Durchsuche

function url_exists($url)
{

<code>$ch = curl_init();     
curl_setopt($ch, CURLOPT_URL,$url);     
curl_setopt($ch, CURLOPT_NOBODY, 1); // 不下载     
curl_setopt($ch, CURLOPT_FAILONERROR, 1);     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
return (curl_exec($ch)!==false) ? true : false;     </code>

}

用这个函数判断网址是否存在,传入的url如果是字母能很快判断是否存在,传入的url是中文,判断会变得很慢,为什么?请问有什么改进方法?

回复内容:

function url_exists($url)
{

<code>$ch = curl_init();     
curl_setopt($ch, CURLOPT_URL,$url);     
curl_setopt($ch, CURLOPT_NOBODY, 1); // 不下载     
curl_setopt($ch, CURLOPT_FAILONERROR, 1);     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
return (curl_exec($ch)!==false) ? true : false;     </code>

}

用这个函数判断网址是否存在,传入的url如果是字母能很快判断是否存在,传入的url是中文,判断会变得很慢,为什么?请问有什么改进方法?

供参考 http://blog.csdn.net/ashelyhss/article/details/7916970

希望有所帮助~ :)

如果用curl判断的话 先自行设置规则过滤非url

然后根据返回的headers信息判断url是否存在

其次有个很方便的函数 get_headers() 非url会返回false 正常的url会返回headers信息 然后判断下http状态就行了

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn