Home >Backend Development >PHP Tutorial >PHP下判断网址是否有效的代码_PHP

PHP下判断网址是否有效的代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:14:34771browse
复制代码 代码如下:
$url = ‘http://www.baidu.com';
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
if(false == $contents)
{
echo ‘Curl error: ‘ . curl_error($ch);
}
else
{
….
}

另外,可以用
复制代码 代码如下:
curl_getinfo($ch, CURLINFO_HTTP_CODE);

获取HTTP头文件返回的代码,如果为200,则url可正常访问,不过这个函数必须在 curl_exec() 之后使用,似乎有点多余了。
Statement:
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