Home > Backend Development > PHP Tutorial > curl判断网址是否存在

curl判断网址是否存在

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:16:17
Original
1739 people have browsed it

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>
Copy after login
Copy after login

}

用这个函数判断网址是否存在,传入的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>
Copy after login
Copy after login

}

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

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

希望有所帮助~ :)

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template