PHP下判断网址是否有效的代码_php技巧

WBOY
Release: 2016-05-17 09:15:22
Original
852 people have browsed it
复制代码 代码如下:

$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() 之后使用,似乎有点多余了。
Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template