我试图用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”


当前回答

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

其他回答

如果你在Guzzle中使用PHP 5.6, Guzzle已经切换到使用PHP库自动检测证书,而不是它的进程(参考)。PHP在这里概述了这些变化。

找出PHP/Guzzle在哪里寻找证书

你可以使用下面的PHP命令转储PHP正在寻找的地方:

 var_dump(openssl_get_cert_locations());

获取证书包

对于OS X测试,可以先使用homebrew安装openssl,再使用openssl.cafile=/usr/local/etc/openssl/cert。在您的php.ini或Zend服务器设置(在OpenSSL下)中的pem。

证书包也可以从curl/Mozilla的curl网站上获得:https://curl.haxx.se/docs/caextract.html

告诉PHP证书在哪里

一旦你有了一个bundle,要么把它放在PHP已经在寻找的地方(上面你已经发现了),要么更新openssl。php.ini中的Cafile。(一般在Unix上是/etc/php.ini或/etc/php/7.0/cli/php.ini或/etc/php/php.ini)

如何解决这个问题:

下载并提取cacert。请参考https://curl.se/docs/caextract.html上的说明 将它保存在文件系统的某个地方(例如,XAMPP用户可能使用C:\ XAMPP \php\extras\ssl\cacert.pem) 在你的php.ini中,把这个文件的位置放在[curl]部分(把它放在[openssl]部分也是一个好主意):

[curl]
curl.cainfo = "C:\xampp\php\extras\ssl\cacert.pem"

[openssl]
openssl.cafile = "C:\xampp\php\extras\ssl\cacert.pem"

重启您的web服务器(例如Apache)和PHP FPM服务器(如果适用)

(参考:https://laracasts.com/discuss/channels/general-discussion/curl-error-60-ssl-certificate-problem-unable-to-get-local-issuer-certificate)

请确保您直接通过windows资源管理器打开php.ini文件。(在我的例子中:C:\DevPrograms\wamp64\bin\php\php5.6.25)。

不要在系统托盘的Wamp/Xamp图标菜单中使用php.ini的快捷方式。这个快捷方式在这种情况下不起作用。

然后编辑php.ini:

curl.cainfo ="C:/DevPrograms/wamp64/bin/php/cacert.pem" 

and

openssl.cafile="C:/DevPrograms/wamp64/bin/php/cacert.pem"

保存php.ini后,您不需要在Wamp图标中“重新启动所有服务”或关闭/重新打开CMD。

我刚刚在使用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中太难做到了。

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

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