我试图用Stripe发送一个API请求,但得到错误消息:

cURL错误60:SSL证书问题:无法获得本地颁发者证书

这是我正在运行的代码:

public function chargeStripe()
{
    $stripe = new Stripe;
    $stripe = Stripe::make(env('STRIPE_PUBLIC_KEY'));

    $charge = $stripe->charges()->create([
        'amount'   => 2900,
        'customer' => Input::get('stripeEmail'),
        'currency' => 'EUR',
    ]);

    return Redirect::route('step1');
}

我在谷歌上搜索了很多,很多人建议我下载这个文件:cacert。Pem,把它放在某处,并在我的php.ini中引用它。这是我的php.ini中的部分:

curl.cainfo = "C:\Windows\cacert.pem"

然而,即使在重新启动服务器几次并更改路径之后,我仍然得到相同的错误消息。

我在Apache中启用了ssl_module,在我的php.ini中启用了php_curl。

我还尝试了这个修复:如何修复PHP CURL错误60 SSL

这表明我将这些行添加到我的cURL选项:

curl_setopt($process, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, true);

我在哪里添加选项到我的cURL?显然不是通过命令行,因为我的CLI没有找到命令“curl_setopt”


当前回答

我所做的是使用var_dump(openssl_get_cert_locations());死;在任何PHP脚本中,它给了我关于本地PHP正在使用的默认值的信息:

array (size=8)
  'default_cert_file' => string 'c:/openssl-1.0.1c/ssl/cert.pem' (length=30)
  'default_cert_file_env' => string 'SSL_CERT_FILE' (length=13)
  'default_cert_dir' => string 'c:/openssl-1.0.1c/ssl/certs' (length=27)
  'default_cert_dir_env' => string 'SSL_CERT_DIR' (length=12)
  'default_private_dir' => string 'c:/openssl-1.0.1c/ssl/private' (length=29)
  'default_default_cert_area' => string 'c:/openssl-1.0.1c/ssl' (length=21)
  'ini_cafile' => string 'E:\xampp\php\extras\ssl\cacert.pem' (length=34)
  'ini_capath' => string '' (length=0)

正如你所注意到的,我已经设置了ini_cafile或ini选项curl. cannfo。但在我的情况下,curl将尝试使用“default_cert_file”,它并不存在。

我将文件从https://curl.haxx.se/ca/cacert.pem复制到“default_cert_file”的位置(c:/openssl-1.0.1c/ssl/cert.pem),我能够让它工作。

这是我唯一的解决办法。

其他回答

Wamp/Wordpress/windows用户请注意。我有这个问题几个小时了,甚至没有正确的答案是为我做的,因为我编辑了错误的php.ini文件,因为这个问题是回答XAMPP而不是WAMP用户,即使这个问题是为WAMP。

以下是我所做的:

Download the certificate bundle. Put it inside of C:\wamp64\bin\php\your php version\extras\ssl Inside of C:\wamp64\bin\apache\apache(version)\modules, make sure the file mod_ssl.so is there Inside of Apache directory C:\wamp64\bin\apache\apache2.4.27\conf, enable mod_ssl in httpd.conf Enable php_openssl.dll in php.ini. Be aware my problem was that I had two php.ini files and I need to do this in both of them. First one can be located inside of your WAMP taskbar icon here. And the other one is located in C:\wamp64\bin\php\php(Version) Find the location for both of the php.ini files and find the line curl.cainfo = and give it a path like this curl.cainfo = "C:\wamp64\bin\php\php(Version)\extras\ssl\cacert.pem" Now save the files and restart your server and you should be good to go

当你在使用Windows时,我认为你的路径分隔符是'\'(在Linux上是'/')。 尝试使用常量DIRECTORY_SEPARATOR。您的代码将更易于移植。

Try:

curl_setopt($process, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacert.pem');

EDIT:并写入完整路径。我有一些相对路径的问题(也许curl是从另一个基目录执行的?)

我刚刚在使用guzzlehttp/guzzle composer包的Laravel 4 php框架中遇到了同样的问题。出于某种原因,mailgun的SSL证书突然停止验证,我收到了同样的“错误60”消息。

如果像我一样,您在一个共享主机上,没有访问php.ini的权限,那么其他解决方案是不可能的。在任何情况下,Guzzle有这样的客户端初始化代码,很可能会使php.ini效果无效:

// vendor/guzzlehttp/guzzle/src/Client.php
    $settings = [
        'allow_redirects' => true,
        'exceptions'      => true,
        'decode_content'  => true,
        'verify'          => __DIR__ . '/cacert.pem'
    ];

在这里,Guzzle强制使用自己的内部cacert。pem文件,这个文件现在可能已经过时了,而不是使用cURL环境提供的文件。修改这一行(至少在Linux上)配置Guzzle使用cURL的默认SSL验证逻辑,并解决了我的问题:

由于vendor中的文件并不意味着被篡改,一个更好的解决方案是在使用时配置Guzzle客户端,但这在Laravel 4中太难做到了。

希望这为其他人节省了几个小时的调试时间……

这可能是一个边缘情况,但在我的情况下,问题不是客户端配置(我已经有卷曲。在php.ini中配置了cfo),而不是远程服务器没有正确配置:

它没有在链中发送任何中间证书。没有错误浏览网站使用Chrome,但与PHP我得到以下错误。

卷曲度错误60

在远程web服务器配置中包含中级证书后,它工作了。

您可以使用此站点检查服务器的SSL配置:

https://whatsmychaincert.com/

有一天,当Guzzle(5)脚本试图通过SSL连接到主机时,我突然出现了这个问题。当然,我可以在Guzzle/Curl中禁用验证选项,但这显然不是正确的方法。

我在类似的线程中尝试了这里列出的所有内容,然后最终使用openssl进入终端,对我试图连接的域进行测试:

openssl s_client -connect example.com:443 

... 收到的前几行是:

CONNECTED(00000003)
depth=0 CN = example.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = example.com
verify error:num=21:unable to verify the first certificate
verify return:1 

... 当尝试其他目的地(如:google.com等)时,一切都很好

这促使我联系了我一直试图连接的域名,事实上,他们在他们的一端有一个问题,已经悄然出现。问题解决了,我的剧本又开始运作了。

所以…如果您非常着急,请尝试openssl,并查看您试图连接的位置的响应是否有任何变化。也许有时候这个问题并不那么“地方性”。