PHP CURL和HTTPS

我发现此功能确实出色(IMHO)

/**

* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an

* array containing the HTTP server response header fields and content.

*/

function get_web_page( $url )

{

$options = array(

CURLOPT_RETURNTRANSFER => true, // return web page

CURLOPT_HEADER => false, // don't return headers

CURLOPT_FOLLOWLOCATION => true, // follow redirects

CURLOPT_ENCODING => "", // handle all encodings

CURLOPT_USERAGENT => "spider", // who am i

CURLOPT_AUTOREFERER => true, // set referer on redirect

CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect

CURLOPT_TIMEOUT => 120, // timeout on response

CURLOPT_MAXREDIRS => 10, // stop after 10 redirects

);

$ch = curl_init( $url );

curl_setopt_array( $ch, $options );

$content = curl_exec( $ch );

$err = curl_errno( $ch );

$errmsg = curl_error( $ch );

$header = curl_getinfo( $ch );

curl_close( $ch );

$header['errno'] = $err;

$header['errmsg'] = $errmsg;

$header['content'] = $content;

return $header;

}

我唯一的问题是它不适用于https://。Anny的想法,我需要做些什么才能使https工作?谢谢!

回答:

快速修复,在您的选项中添加:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)

现在,您不知道您实际上要连接到哪个主机,因为cURL不会以任何方式验证证书。

或只是将其添加到当前函数中:

/**

* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an

* array containing the HTTP server response header fields and content.

*/

function get_web_page( $url )

{

$options = array(

CURLOPT_RETURNTRANSFER => true, // return web page

CURLOPT_HEADER => false, // don't return headers

CURLOPT_FOLLOWLOCATION => true, // follow redirects

CURLOPT_ENCODING => "", // handle all encodings

CURLOPT_USERAGENT => "spider", // who am i

CURLOPT_AUTOREFERER => true, // set referer on redirect

CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect

CURLOPT_TIMEOUT => 120, // timeout on response

CURLOPT_MAXREDIRS => 10, // stop after 10 redirects

CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks

);

$ch = curl_init( $url );

curl_setopt_array( $ch, $options );

$content = curl_exec( $ch );

$err = curl_errno( $ch );

$errmsg = curl_error( $ch );

$header = curl_getinfo( $ch );

curl_close( $ch );

$header['errno'] = $err;

$header['errmsg'] = $errmsg;

$header['content'] = $content;

return $header;

}

以上是 PHP CURL和HTTPS 的全部内容, 来源链接: utcz.com/qa/402119.html

回到顶部