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

标签 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}

最佳答案

当您的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"

关于php - 无法在“file_get_contents()”中加载cafile流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55526568/

相关文章:

php - 是否可以向对一个域源具有 CORS 限制的端点发出 Guzzle 请求?

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - 通过引用翻译变量替换文本中的占位符

google-maps - Google 地理编码 API 为邮政编码返回 ZERO_RESULTS

ios - 如何通过 google maps iOS API 对地址进行地理编码?

php - 使用 ajax 在基于 mvc 的 php 应用程序的下拉列表中填充城市?

php - 尝试添加表时出现错误#1064

javascript - 我可以按城镇限制谷歌地理编码器的结果吗?

geocoding - Google geocode API 返回不确定的结果

google-maps - Google 地理编码 API 找不到地点 API 返回的地址