【问题标题】:cURL connection to a https pagecURL 连接到 https 页面
【发布时间】:2014-06-30 22:53:22
【问题描述】:

所以我有这个代码:

$url = "https://..."; //a valid url, showing without errors a correct XML

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);

/* to which i added the following */
curl_setopt($handle, CURLOPT_CAPATH, "http://curl.haxx.se/ca/cacert.pem");
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, TRUE);

$result = curl_exec($handle);
$resultXml = new SimpleXMLElement($result); //Line 47

/* tests */
var_dump($handle); echo "<br/><br/>"; //resource(11) of type (curl) 
var_dump($url);    echo "<br/><br/>"; //string(149) "https://..." (exactly $url)
var_dump($result); echo "<br/><br/>"; //bool(false)

这会产生这个错误:

Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' [...] on line 47
Stack trace:
#0 C:\yy.php(47): SimpleXMLElement->__construct('')
#1 C:\xx.php(23): Integrator->Integrator('numbers', Object(Database))
#2 {main}
thrown [...] on line 47

var_dump() 返回的是 cmets。

使用它,我们可以看到 $result 是空的;这是主要问题!

我一开始以为问题出在页面的https连接上,所以在中间加了3行;问题依旧。

现在我想知道我的 SSL 证书(来自 Gandi SAS)是否没有明确包含在 http://curl.haxx.se/ca/cacert.pem 中是问题所在。 如果是这样,我如何告诉 CURLOPT_CAPATH 获取我的证书?

其他地方有错误吗?

感谢您的帮助!

【问题讨论】:

    标签: php curl ssl https


    【解决方案1】:

    使用 CURLOPT_CAINFO 指定证书文件。

    CURLOPT_CAINFO - The name of a file holding one or more certificates to
    verify the peer with. This only makes sense when used in combination with
    CURLOPT_SSL_VERIFYPEER.
    

    这需要是文件的绝对路径。

    修复 ssl 问题后,您应该在 curl 响应中获取数据。

    【讨论】:

    • 我尝试过使用 CURLOPT_CAINFO,但顺序重要吗?到目前为止,没有什么新的错误。
    • 看起来我们这里有一个相关的主题可能有用。 stackoverflow.com/questions/12305157/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多