从Google Geolocation API获取MAC地址,PHP / JSON的正确响应

时间:2018-04-04 19:31:06

标签: php google-maps curl google-geolocation

我目前正在开发一个门户网站,以便使用Google Geo-location API,特别是针对MAC地址。

我正在使用PHP和cURL发送API请求,从谷歌解码返回的JSON并进行测试,以回应结果。

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

我遇到的问题是返回的位置结果从不与MAC地址的位置相关,而是始终与用于发送请求的IP地址相关。准确度数字也总是非常高(不准确)。

我使用地址中提供的建议谷歌输入进行了测试:

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

我可以使用VPN可靠地改变API返回的结果。我也确保我的请求包括'thinkIP'是假的。如果没有找到地理定位数据,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);
}

1 个答案:

答案 0 :(得分: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();