Php 在“中加载cafile流失败”;文件“获取内容()”;?

Php 在“中加载cafile流失败”;文件“获取内容()”;?,php,google-geocoding-api,Php,Google Geocoding Api,我试图从谷歌地理编码api获取json数据。但是,php警告显示了一些关于“加载cafile流失败”的错误 这是我的密码: $apiKey = 'apikey'; $address = urlencode( '1600 Amphitheatre Pkwy, Mountain View, CA 94043' ); $url = "https://maps.googleapis.com/maps/api/geocode/json?address= {$address}key={

我试图从谷歌地理编码api获取json数据。但是,php警告显示了一些关于“加载cafile流失败”的错误

这是我的密码:

$apiKey  = 'apikey';
$address = urlencode( '1600 Amphitheatre Pkwy, Mountain View, CA 94043' 
);
$url     = "https://maps.googleapis.com/maps/api/geocode/json?address= 
    {$address}key={apiKey}";
$resp    = json_decode( file_get_contents( $url ), true );
echo $url;

$lat    = $resp['results'][0]['geometry']['location']['lat'] ?? '';
$long   = $resp['results'][0]['geometry']['location']['lng'] ?? '';
以下是错误:

PHP Warning:  failed loading cafile stream: `C:\xampp\apache\bin\curl-ca- 
bundle.crt' in C:\Users\1\Desktop\test.php on line 7

Warning: failed loading cafile stream: `C:\xampp\apache\bin\curl-ca- 
bundle.crt' in C:\Users\1\Desktop\test.php on line 7
PHP Warning:  file_get_contents(): Failed to enable crypto in 
C:\Users\1\Desktop\test.php on line 7

Warning: file_get_contents(): Failed to enable crypto in 
C:\Users\1\Desktop\test.php on line 7
PHP Warning:  
file_get_contents(https://maps.googleapis.com/maps/api/geocode/json? 
address=1600+Amphitheatre+Pkwy%2C+Mountain+Vi
ew%2C+CA+94043key={apiKey}): failed to open stream: operation failed in 
C:\Users\1\Desktop\test.php on line 7

Warning: 
file_get_contents(https://maps.googleapis.com/maps/api/geocode/json? 
address=1600+Amphitheatre+Pkwy%2C+Mountain+View%2C
+CA+94043key={apiKey}): failed to open stream: operation failed in 
C:\Users\1\Desktop\test.php on line 7
https://maps.googleapis.com/maps/api/geocode/json? 
address=1600+Amphitheatre+Pkwy%2C+Mountain+View%2C+CA+94043key={apiKey}

当php.ini文件的curl.cainfoopenssl.cafile配置属性未针对任何允许您创建ssl连接的有效证书时,就会发生此错误,因为它们将无效

这是在使用CURL作为示例调用外部服务器的请求时发生的

您必须从链接加载casert.pem

并将证书文件放置在以下路径中:

C:\xampp\apache\bin\cacert.pem

之后,检查名为:curl.cainfoopenssl.cafile的配置键上的php.ini,并进行此配置

例如:

curl.cainfo = "C:\xampp\apache\bin\cacert.pem"
openssl.cafile = "‪C:\xampp\apache\bin\cacert.pem"

除了Mohammed Yassine CHABLI的anwser。更改php.ini中的文件名和路径并不能解决这个问题


我不得不将“cacert.pem”重命名为“curl ca bundle.crt”以使其正常工作。

对于MacOS用户
brew安装openssl
为我解决了这个问题

您只需走C:\xampp\perl\vendor\lib\Mozilla\ca>路线即可 并复制名为cacert.pem的文件

然后输入php.ini文件并在[curl]中粘贴以下路径C:\xampp\perl\vendor\lib\Mozilla\CA\cacert.pem --curl.cainfo=“C:\xampp\perl\vendor\lib\Mozilla\CA\cacert.pem”

您将在[openssl]中执行同样的操作
--openssl.cafile=“C:\xampp\perl\vendor\lib\Mozilla\CA\cacert.pem”

我想你的意思是第二行的
openssl.cafile
。我在使用SoapClient时遇到了同样的问题。上述解决方案与xampp(windows)配合使用效果良好。在php.ini更改通常使用代码格式后,或许可以在xampp控制面板上重新启动apache。它将提供更好的可读性。