【问题标题】:PHP CURL WAMP - SSL certificate error: unable to get local issuer certificatePHP CURL WAMP - SSL 证书错误:无法获取本地颁发者证书
【发布时间】:2017-09-17 03:05:35
【问题描述】:

我正在运行 PHP 5.5.12 作为 WAMP 的一部分。

当我尝试执行此代码时,我收到以下错误: SSL 证书错误:无法获取本地颁发者证书 该脚本旨在通过登录名和密码从认证表单中获取内容。

<?php

include('simple_html_dom.php');
ini_set('max_execution_time', 0);
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2G');

class EmailScraping {

    public $url;

    public function __construct($url) {
        if ($this->validUrl($url))
            $this->url = $url;
        else {
            echo 'bad url <br>';
        }
    }

    public function getContent() {

        $username = "****"; 
        $password = "****";

        //login form action url
        $url = 'https://placement.emploiquebec.gouv.qc.ca/mbe/login/portail/auth.asp?provenance=emplr&CL=french';
        $postinfo = "posted&L_OBLG_NOM_UTIL=Votre code d'utilisation&OBLG_NOM_UTIL=" . $username . "&L_OBLG_MOT_PASSE=Votre mot de passe&OBLG_MOT_PASSE=" . $password . "&continuer=CONTINUER&LastLogin&NbEssai=0";

        $cookie_file_path = "./cookies.txt";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_ENCODING , "");
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        //set the cookie the site has for certain features, this is optional
        curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);        
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);    
        curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\cacert.pem');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);       
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
        $html1 = curl_exec($ch);
        curl_close($ch);

        $ch = curl_init();

        //page with the content I want to grab
        curl_setopt($ch, CURLOPT_URL, "https://placement.emploiquebec.gouv.qc.ca/mbe/ut/suivroffrs/esuivroffrs.asp?CL=french");
        curl_setopt($ch, CURLOPT_POST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        $answer = curl_exec($ch);

        if (curl_error($ch)) {
            echo curl_error($ch);
        }

        curl_close($ch);
        return $answer;
    }

    public function validUrl($url) {

        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
            return false;
        }
        return true;
    }

}

$scraper = new EmailScraping('https://www.facebook.com');
$result = $scraper->getContent();
$html = "<base href='https://placement.emploiquebec.gouv.qc.ca/mbe/login/portail/auth.asp?provenance=emplr&CL=french' />" . $result;
echo $html;

我正在尝试将此代码添加到 php.ini 文件中并重新启动 WAMP 服务器,但仍然出现相同的错误。

curl.cainfo="C:/wamp/www/domscraper/cacert.pem"

我真的不知道还能尝试什么。

谁能建议我还能尝试什么?

【问题讨论】:

  • curl.cainfo="C:/wamp/www/domscraper/cacert.pem" 是否存在正确的路径/文件?
  • 是的,这个路径/文件存在@Antonis Tsimourtos
  • 也许尝试下载并替换为this

标签: php curl dom web-scraping wamp


【解决方案1】:

你有curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

尝试将其设置为false

这是一些文档的链接 - http://php.net/manual/en/function.curl-setopt.php

这应该只是开发,在产品上你可能应该解决任何证书问题,并且可能需要从提供者那里获取证书/密钥。

对于错误日志添加(并查看文档以获取更多信息)

$verbose = fopen ( 'php://temp', 'w+' );
curl_setopt ( $curlManager, CURLOPT_VERBOSE, true );
curl_setopt ( $curlManager, CURLOPT_STDERR, $verbose );

如果不需要调试,您也可以尝试删除其中一些选项。

【讨论】:

  • 我正在尝试使用 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) 但仍然收到相同的错误。我只是想从登录表单中获取内容并传递用户名和密码。请提供任何帮助。
  • 编辑答案以包含错误日志,而不是将其放在评论中。
【解决方案2】:

这是最糟糕的配置:从 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 到 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

我一直在尝试连接 Postman 制作的 API,它可以工作。

我在 64 位 Windows 上使用 WAMP (PHP7+Apache)。

【讨论】:

  • 这不是解决方案,而是临时补丁。
  • 这不是一个修复,它是一种解决方法,它告诉 cURL 忘记安全性。仅适用于开发环境(即便如此,也要谨慎)。
猜你喜欢
  • 2017-07-19
  • 2016-07-06
相关资源
最近更新 更多