【问题标题】:How to scrape a SSL or HTTPS URL如何抓取 SSL 或 HTTPS URL
【发布时间】:2015-09-18 16:36:48
【问题描述】:

我编写了一个使用 CURL 抓取网站的函数,但它在调用时什么也不返回,并且不明白为什么。输出为空

  <?php
    function scrape($url)
    {
        $headers = Array(
                    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                    "Cache-Control: max-age=0",
                    "Connection: keep-alive",
                    "Keep-Alive: 300",
                    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                    "Accept-Language: en-us,en;q=0.5",
                    "Pragma: "
                );
        $config = Array(
                        CURLOPT_RETURNTRANSFER => TRUE ,
                        CURLOPT_FOLLOWLOCATION => TRUE ,
                        CURLOPT_AUTOREFERER => TRUE ,
                        CURLOPT_CONNECTTIMEOUT => 120 ,
                        CURLOPT_TIMEOUT => 120 ,
                        CURLOPT_MAXREDIRS => 10 ,                   
                        CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8" ,
                        CURLOPT_URL => $url ,
                       ) ;
        $handle = curl_init() ;
        curl_setopt_array($handle,$config) ;
        curl_setopt($handle,CURLOPT_HTTPHEADER,$headers) ;
        $data = curl_exec($handle) ;
        curl_close($handle) ;
        return $data ;
    }

    echo scrape("https://www.google.com") ;
?>

【问题讨论】:

    标签: php curl web-scraping


    【解决方案1】:

    尝试抓取 ssl 或 https url 时有 2 个可能的修复方法:

    1. 快速修复
    2. 正确的修复方法

    快速修复,首先。

    警告:这可能会引入 SSL 旨在防止的安全问题。

    设置:CURLOPT_SSL_VERIFYPEER =&gt; false

    第二个,也是正确的修复。设置 3 个选项:

    1. CURLOPT_SSL_VERIFYPEER =&gt; true
    2. CURLOPT_SSL_VERIFYHOST =&gt; 2
    3. CURLOPT_CAINFO =&gt; getcwd() . '\CAcert.pem'

    您需要做的最后一件事是下载 CA 证书。

    转到,-http://curl.haxx.se/docs/caextract.html-> 单击“cacert.pem”-> 将文本复制/粘贴到文本编辑器中-> 将文件另存为“CAcert.pem”检查它不是“CAcert.pem” .txt'

    <?php
        function scrape($url)
        {
            $headers = Array(
                        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                        "Cache-Control: max-age=0",
                        "Connection: keep-alive",
                        "Keep-Alive: 300",
                        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                        "Accept-Language: en-us,en;q=0.5",
                        "Pragma: "
                    );
            $config = Array(
                            CURLOPT_SSL_VERIFYPEER => true,
                            CURLOPT_SSL_VERIFYHOST => 2,
                            CURLOPT_CAINFO => getcwd() . '\CAcert.pem',
                            CURLOPT_RETURNTRANSFER => TRUE ,
                            CURLOPT_FOLLOWLOCATION => TRUE ,
                            CURLOPT_AUTOREFERER => TRUE ,
                            CURLOPT_CONNECTTIMEOUT => 120 ,
                            CURLOPT_TIMEOUT => 120 ,
                            CURLOPT_MAXREDIRS => 10 ,                   
                            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8" ,
                            CURLOPT_URL => $url
                           ) ;
            $handle = curl_init() ;
            curl_setopt_array($handle,$config) ;
            curl_setopt($handle,CURLOPT_HTTPHEADER,$headers) ;
            $output->data = curl_exec($handle) ;
    
            if(curl_exec($handle) === false) {
                $output->error = 'Curl error: ' . curl_error($handle);
            } else {
                $output->error = 'Operation completed without any errors';
            }
    
            curl_close($handle) ;
            return $output ;
        }
    
    $scrape = scrape("https://www.google.com") ;
    
    echo $scrape->data;
    
    //uncomment for errors
    //echo $scrape->error;
    ?>
    

    【讨论】:

    • 我现在收到此错误 Curl 错误:错误设置证书验证位置:CAfile: C:\xampp\htdocs\php_inc\CAcert.pem CApath: none
    • 可能是几件事。但是,让我们从您的路径开始,将该 url 复制/粘贴到您的浏览器中,它找到文件了吗?如果没有,你没有使用正确的路径。
    • 我使用了file_exists(),它成功了。路径存在
    • 我的意思是你的 CAcert 的路径。 pem 文件
    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多