【问题标题】:Google Geocode returns blank string on remote server - works fine locally谷歌地理编码在远程服务器上返回空白字符串 - 在本地工作正常
【发布时间】:2017-08-12 10:00:44
【问题描述】:

我正在尝试实现一项获取所提供地址的 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 行的注释,第一行返回了单词“Data”,后面没有任何内容。接下来的三行,预计只是返回了相应的'pre'标签。

检查了控制台日志,没有显示错误。我在服务器上尝试了一个快速的客户端脚本,它似乎工作正常。

【问题讨论】:

    标签: php google-maps-api-3


    【解决方案1】:

    这最终是因为远程服务器上禁用了“allow_url_fopen”设置,从而有效地禁用了我的函数的“file_get_contents”部分。我可以通过主机的 phpinfo 页面检查这一点,但也可以通过how to check if allow_url_fopen is enabled or not 声明的Marcin Orlowski 完成。

    我可以通过修改函数以使用 cURL 来解决此问题。

    我对 cURL 基本上一无所知,但我在我的函数中替换了这一行:

    $data = file_get_contents($BaseURL.str_ireplace(" ","+",$Address));
    

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $BaseURL.str_ireplace(" ","+",$Address));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    

    虽然它不再适用于我的本地机器(因为我没有安装/配置 cURL),但它适用于远程服务器,这是最重要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多