【发布时间】: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