Ada*_*nga 3 php api json curl google-maps
所以,我正在尝试使用cURL获取API数据,但是我从下面的代码中的else语句中得到消息"fail".API调用是用于获取坐标的Google地理编码.
代码:
<?php
require_once('../db.php');
$api_key = "somekey";
$sqlQuery = mysql_query("SELECT `County` FROM `table`");
$ch = curl_init();
/* Fetch county */
while($rows = mysql_fetch_array($sqlQuery)) {
$countyArr = $rows['County'];
/* Call google API and save coordinates for each county */
curl_setopt ($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address=".$countyArr.",+CA&key=".$api_key."");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$json= curl_exec($ch, true);
$obj = json_decode($json);
if ($obj->status == "OK") {
$lat = $obj->results->location->lat;
$lng = $obj->results->location->lng;
echo $lat;
} else {
echo "fail";
}
}
curl_close($ch);
?>
Run Code Online (Sandbox Code Playgroud)
get_file_contents()
早点使用,但似乎我的托管已停用该功能.allow_url_fopen = on
到php.ini没有做到这一点.编辑:
$obj->status
而$obj->results->location->lat
在其他声明中,没有任何表现.所以$obj
似乎是NULL在验证证书时它似乎失败,您可以禁用CA验证
要关闭证书验证(对等和主机验证),请设置以下选项:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Run Code Online (Sandbox Code Playgroud)
例
如何禁用证书验证
$address = "Ontario,CA";
$apiKey = "";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address; //. "&key=" . $apiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
print json_encode($result, JSON_PRETTY_PRINT);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2652 次 |
最近记录: |