我试图从谷歌地理编码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作为示例调用对外部服务器的请求时,会发生这种情况。
你必须从链接qazxsw poi下载cacert.pem
并将认证文件放在路径中:
C:\ xampp \ apache \ bin \ cacert.pem
之后,检查名为curl.cainfo和openssl.cafile的配置密钥上的php.ini并进行此配置
例如:
here
除了穆罕默德Yassine CHABLI的anwser。更改php.ini中的文件名和路径并没有解决它。
我不得不将'cacert.pem'重命名为'curl-ca-bundle.crt'以使其正常工作。