时间:2021-07-01 10:21:17 帮助过:7人阅读
$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() 之后使用,似乎有点多余了。http://www.bkjia.com/PHPjc/324385.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324385.htmlTechArticle 代码如下:
$url = ‘http://www.baidu.com'; $ch = curl_init(); $timeout = 10; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt...