为了账号安全,请及时绑定邮箱和手机立即绑定

PHP curl&HTTPS

PHP curl&HTTPS

PHP
慕莱坞森 2019-08-03 13:03:06
PHP curl&HTTPS我发现这个功能做得很棒(IMHO):http:/nadeausoftware.com/条文/2007/06/php_tip_ow_get_web_page_use_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     );     $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?谢谢!
查看完整描述

3 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

快速修复,将其添加到您的选项中:

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;}





查看完整回答
反对 回复 2019-08-04
  • 3 回答
  • 0 关注
  • 487 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信