【问题标题】:Failed loading cafile stream in "file_get_contents()"?在“file_get_contents()”中加载 cafile 流失败?
【发布时间】:2019-04-05 00:09:45
【问题描述】:

我试图从 google geocoding 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 google-geocoding-api


    【解决方案1】:

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

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

    您必须从链接here 下载 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"
    

    【讨论】:

    • 我认为您的意思是第二行中的openssl.cafile
    • 我在使用 SoapClient 时遇到了同样的问题。上述解决方案适用于 xampp ( windows )
    【解决方案2】:

    除了 Mohammed Yassine CHABLI 的 anwser。更改 php.ini 中的文件名和路径并没有为我解决。

    我必须将“cacert.pem”重命名为“curl-ca-bundle.crt”才能使其正常工作。

    【讨论】:

    • 也许在 php.ini 更改后重新启动 xampp 控制面板上的 apache 即可
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      你只需要去你的 ROUTE 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"

      【讨论】:

      • 始终使用代码格式。它将提供更好的可读性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2013-04-29
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      相关资源
      最近更新 更多