今天写一个采集页面,用PHP CURL https 竟然报错,搜集了一些资料,完成了 PHP CURL 获取HTTPS网页源码的函数。
function GetHtml( $url ) {
$ch = curl_init( );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_REFERER, ‘http://www.baidu.com/’ );
curl_setopt ( $ch, CURLOPT_USERAGENT, ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.6; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)’ );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 1); // ssl 访问核心参数
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE); // ssl 访问核心参数
$code = curl_exec( $ch );
curl_close( $ch );
if ( empty( $code ) or stristr( $code, ‘<title>403 Forbidden</title>’ ) or stristr( $code, "<hr><center>nginx" ) or stristr( $code, "<title>Error</title>" ) or stristr( $code, "<TITLE>无法找到该页</TITLE>" ) ){
exit( );
} else {
return $code;
}
}