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


当前回答

如果你在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)

其他回答

我所做的是使用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),我能够让它工作。

这是我唯一的解决办法。

当我运行'var_dump(php_ini_loaded_file());' 我在页面上得到这个输出 “C: \开发\ bin \ apache \ apache2.4.33 \ bin \ php . ini”(长度= 50)'

为了让php加载我的证书文件,我必须在这个路径下编辑php.ini' C:\Development\bin\apache\apache2.4.33\bin\php.ini' 添加openssl.cafile="C:/Development/bin/php/php7.2.4/extras/ssl/cacert. exe "。pem”,我从https://curl.haxx.se/docs/caextract.html下载并放置了证书文件

我在Windows 10,使用drupal 8, wamp和php7.2.4

cartalyst/stripe使用的Guzzle,将执行以下操作来查找合适的证书存档来检查服务器证书:

Check if openssl.cafile is set in your php.ini file. Check if curl.cainfo is set in your php.ini file. Check if /etc/pki/tls/certs/ca-bundle.crt exists (Red Hat, CentOS, Fedora; provided by the ca-certificates package) Check if /etc/ssl/certs/ca-certificates.crt exists (Ubuntu, Debian; provided by the ca-certificates package) Check if /usr/local/share/certs/ca-root-nss.crt exists (FreeBSD; provided by the ca_root_nss package) Check if /usr/local/etc/openssl/cert.pem (OS X; provided by homebrew) Check if C:\windows\system32\curl-ca-bundle.crt exists (Windows) Check if C:\windows\curl-ca-bundle.crt exists (Windows)

你需要通过一个简单的测试来确保前两个设置的值是正确定义的:

echo "openssl.cafile: ", ini_get('openssl.cafile'), "\n";
echo "curl.cainfo: ", ini_get('curl.cainfo'), "\n";

或者,尝试将文件写入#7或#8所指示的位置。

请确保您直接通过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。

所有答案都是正确的;但最重要的是你必须找到正确的php.ini文件。 在CMD中检查这个命令“PHP——ini”不是找到正确的PHP .ini文件的正确答案。

如果你编辑

curl.cainfo ="PATH/cacert.pem"

并检查

var_dump(openssl_get_cert_locations()); 

然后旋度。fo应该有一个值。如果不是,那么这是不对的php.ini文件;

*我建议你在wamp/bin或xxamp/bin或任何你使用的服务器中搜索*.ini,逐个更改并检查。*