无法在“ file_get_contents()”中加载cafile流?

时间:2019-04-05 00:09:46

标签: php google-geocoding-api

我正在尝试从Google地理编码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}

4 个答案:

答案 0 :(得分:3)

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

以CURL为例向外部服务器调用请求时,就会发生这种情况。

您必须从链接here

中下载 casert.pem

并将认证文件放在路径中:

  

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

之后,在名为 curl.cainfo openssl.cafile 的配置项上检查php.ini,并进行此配置

示例:

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

答案 1 :(得分:0)

除了穆罕默德(Mohammed Yassine)CHABLI的anwser。更改php.ini中的文件名和路径并不能解决我的问题。

我必须将“ c​​acert.pem”重命名为“ curl-ca-bundle.crt”,以使其正常工作。

答案 2 :(得分:0)

对于MacOS用户brew install openssl为我解决了此问题

答案 3 :(得分:0)

您只需要转到ROUTE C:\ xampp \ perl \供应商\ lib \ Mozilla \ CA 并复制名为cacert.pem的文件

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

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