我有下一个PHP代码
$str = $city . ', ' . $country . ', ' . $addr;
$address = urlencode($str);
$country = urlencode($country);
$json = file_get_contents("https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$country");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
if ($long and $lat) {
$sql = "`lat`='$lat',`lng`='$long',`edit_date`=NOW()";
$up_sql = "UPDATE `user_cards` SET " . $sql . " WHERE `id`='$id'";
}
此代码在90%的情况下成功运行,只有某些地址才会出现此问题。
例如,如果$ str =“Bulgaria,Ravda,Emerald Beach Resort”和$ country =“Bulgaria”,file_get_contents将返回NULL,但如果我将此字符串粘贴到maps.google.com,则google successcesfull会找到此地址。错误在哪里?