我正在尝试实现获取所提供地址的GPS坐标的服务,因此我想出了以下内容,这在本地工作正常。但是,当我尝试在托管服务提供商上实现它时,突然之间该功能失败了,因为Google没有返回任何结果 - 甚至没有空白数组,也没有任何结果。
我认为它是通过网络主机阻止的,但我不确定要查找什么。
功能:
USE yourdb;
}
通过:
调用function getCoordinates($Address, $APIKey) {
$BaseURL = "https://maps.googleapis.com/maps/api/geocode/json?key=".$APIKey."&address=";
$data = @file_get_contents($BaseURL.str_ireplace(" ","+",$Address));
//echo "Data:".$data."<br>";
$jsondata = json_decode($data,true);
//echo "<pre>";
//print_r($jsondata);
//echo "</pre>";
switch($jsondata["status"]) {
case "OK" :
if (count($jsondata["results"]) > 1) {
return array("status" => "FAIL", "message" => "Multiple results were returned");
}
else {
if (isset($jsondata["results"][0]["geometry"]["location"])) {
return array("status" => "SUCCESS","latitude" => $jsondata["results"][0]["geometry"]["location"]["lat"],"longitude" => $jsondata["results"][0]["geometry"]["location"]["lng"]);
}
}
return $jsondata["results"];
break;
case "ZERO_RESULTS" :
return array("status" => "FAIL", "message" => "Zero Results were returned");
break;
case "OVER_QUERY_LIMIT" :
return array("status" => "FAIL", "message" => "API Key is over the daily limit. It will automatically try again tomorrow");
break;
case "REQUEST_DENIED" :
return array("status" => "FAIL", "message" => "Request was denied");
break;
case "INVALID_REQUEST" :
return array("status" => "FAIL", "message" => "Invalid request, typically because the address is missing");
break;
case "UNKNOWN_ERROR" :
return array("status" => "FAIL", "message" => "Unknown error, Request could not be processed due to a Google server error. It may work again if you try later.");
break;
case "ERROR" :
return array("status" => "FAIL", "message" => "Error, the request timed out");
break;
default:
$Message = array("Failure",print_r($jsondata));
return array("status" => "FAIL", "message" => $Message);
break;
}
出于测试目的,我取消注释了靠近函数顶部的4行,并且第一行返回了单词&#39; Data&#39;没有任何关注。接下来的三行,预计只会返回相应的&#39; pre&#39;标签
检查控制台日志,没有指出错误。我在服务器上尝试了一个快速的客户端脚本,它似乎工作正常。