【问题标题】:Obtaining correct response from Google Geolocation API for MAC Address, PHP/JSON从 Google Geolocation API 获取 MAC 地址、PHP/JSON 的正确响应
【发布时间】:2018-09-14 11:42:13
【问题描述】:

我目前正在开发一个 Web 门户以使用 Google 地理位置 API,特别是针对 MAC 地址。

我正在使用 PHP 和 cURL 发送 API 请求,解码从谷歌返回的 JSON 并出于测试目的,回显结果。

我目前已成功将请求提交到 Google 的 API 并收到没有错误的结果的代码。

我遇到的问题是返回的位置结果与 MAC 地址的位置无关,而总是与用于发送请求的 IP 地址有关。准确率也总是很高(不准确)。

我已经使用地址给出的建议的谷歌输入测试了这个:

https://developers.google.com/maps/documentation/geolocation/intro#sample-requests

我可以使用 VPN 可靠地更改 API 返回的结果。我还确保我的请求包括“considerIP”是错误的。如果没有找到地理位置数据,API 应该返回 404 错误,但是我总是得到 200 个代码和结果。

据我所知,我的 cURL 请求是正确的,我完全不知道为什么 API 在使用他们自己建议的示例时会返回有关我的 IP 地址的信息。

代码如下:

function askGoogle(){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/geolocation/v1/geolocate?key=[MYAPIKEY]");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $JSONArray = array (
        'considerIp' => 'false',
        'wifiAccessPoints' => 
            array (
                0 => 
                array (
                    'macAddress' => '00:25:9c:cf:1c:ac',
                ),
            1 => 
                array (
                    'macAddress' => '00:25:9c:cf:1c:ad',
                ),
            ),
    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($JSONArray));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "Content-Length: 0"));

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "The curl completion is ".$httpCode."<br>";

    $message =  json_decode($result, true); //converts the returned JSON message into an array.

    print_r(array_values($message));
    echo "<br>";

    curl_close ($ch);
}

【问题讨论】:

    标签: php google-maps curl google-geolocation


    【解决方案1】:

    我让你的代码工作了。我相信您的请求从 API 返回默认的基于 IP 的响应的两个原因是您在数组上使用 http_build_query 而不是 json_encode,并且因为标头中的内容长度为 0 而不是 json 有效负载的字符串长度.

    function askGoogle()
    {
        $ch = curl_init();
    
        curl_setopt(
            $ch,
            CURLOPT_URL,
            "https://www.googleapis.com/geolocation/v1/geolocate?key=[MYAPIKEY]"
        );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $payloadArray = [
            'considerIp' => false,
            'wifiAccessPoints' => [
                [
                    'macAddress'         => '00:25:9c:cf:1c:ac',
                    "signalStrength"     => -25,
                    "signalToNoiseRatio" => -101
                ],
                [
                    'macAddress'         => '00:25:9c:cf:1c:ad',
                    "signalStrength"     => -25,
                    "signalToNoiseRatio" => -101
                ],
            ],
        ];
    
        $payloadJson = json_encode($payloadArray);
    
        curl_setopt(
            $ch,
            CURLOPT_HTTPHEADER,
            ["Content-type: application/json", "Content-Length: ".strlen($payloadJson)]
        );
    
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJson);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
    
    
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
    
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo "The curl completion is ".$httpCode."<br>";
    
        $message =  json_decode($result, true); //converts the returned JSON message into an array.
    
        print_r($message);
        echo "<br>";
    
        curl_close($ch);
    }
    
    askGoogle();
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      相关资源
      最近更新 更多