我想實現一個服務,獲取提供的地址的GPS座標,所以我想出了以下,這在當地工作正常。然而,當我試圖在託管服務提供商上實現它時,突然間該功能失敗了,因爲沒有從Google返回結果 - 甚至沒有空白數組,甚至沒有任何結果。谷歌地理編碼返回在遠程服務器上的空白字符串 - 在本地正常工作
我認爲這是通過網絡主機封鎖,但我不知道什麼,甚至尋找。
功能:
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;
}
}
經由被稱爲:
$LocationCoordinates = getCoordinates("123 Main Street, Toronto, ON", [[$MapsAPIKey]]);
出於測試目的,我未註釋出4行接近函數的頂部和第一返回的單詞'數據'沒有任何關係。接下來的三行,預計只是返回相應的'pre'標籤。
檢查控制檯日誌,並沒有指出錯誤。我在服務器上嘗試了一個快速的客戶端腳本,它似乎工作正常。