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

问题描述 投票:0回答:2

我目前正在开发一个门户网站,以利用 Google 地理位置 API,特别是 MAC 地址。

我正在使用 PHP 和 cURL 发送 API 请求,解码从 google 返回的 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
2个回答
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();

0
投票

我按照这个例子,但无论我添加什么MAC,它都会给出404 not found

HTTP/2 404 
vary: X-Origin
vary: Referer
vary: Origin,Accept-Encoding
content-type: application/json; charset=UTF-8
date: Sun, 06 Oct 2024 10:25:43 GMT
server: scaffolding on HTTPServer2
cache-control: private
x-xss-protection: 0
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
accept-ranges: none

{
  "error": {
    "code": 404,
    "message": "Not Found",
    "errors": [
      {
        "message": "Not Found",
        "domain": "geolocation",
        "reason": "notFound"
      }
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.