Copy code The code is as follows:
$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
{
….
}
In addition, you can use
Copy code The code is as follows:
curl_getinfo($ch, CURLINFO_HTTP_CODE);
Get the code returned by the HTTP header file, If it is 200, the url can be accessed normally, but this function must be used after curl_exec(), which seems a bit redundant.
http://www.bkjia.com/PHPjc/324385.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324385.htmlTechArticleCopy the code as follows: $url = 'http://www.baidu.com'; $ch = curl_init (); $timeout = 10; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt...