try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否将响应结果存入变量,1是存入,0是直接echo
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//在发起连接前等待的时间,如果设置为0,则无限等待。
curl_setopt($ch, CURLOPT_TIMEOUT, 15);//允许执行的最长秒数。
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);//伪造浏览器头信息
curl_setopt($ch, CURLOPT_REFERER,$_REFERER);//伪造来源页面
curl_setopt($ch, CURLOPT_URL, $url);
$htmlSource = curl_exec($ch);
//$htmlSource = mb_convert_encoding($htmlSource, 'utf-8', 'GBK');//如有必要,使用该函数对结果进行转码
if (curl_errno($ch)) {
throw new Exception('获取失败:'.curl_errno($ch), 1);//抛出错误信息
}
else {echo '获取成功'; }
curl_close($ch);
}
catch(Exception $e) {
echo $e->getMessage();
}