从 Google 地理定位 API 获取 MAC 地址的正确响应,PHP/JSON
Obtaining correct response from Google Geolocation API for MAC Address, PHP/JSON
我目前正在开发一个门户网站,以利用 Google 地理位置 API,专门用于 MAC 地址。
我正在使用 PHP 和 cURL 发送对 API 的请求,从 google 解码 returned JSON 并用于测试目的, 呼应结果。
我目前的代码成功向Google的API提交了请求并收到了没有错误的结果。
我遇到的问题是位置结果 returned 从不与 MAC 地址的位置相关,而是始终与用于发送请求的 IP 地址相关。准确率也总是很高(不准确)。
我已经使用建议的 google 输入地址进行了测试:
https://developers.google.com/maps/documentation/geolocation/intro#sample-requests
我可以通过使用 VPN 可靠地更改 return 由 API 编辑的结果。我还确保我的请求包括 'considerIP' 是错误的。如果没有找到地理定位数据,API 应该 return 一个 404 错误,但是我总是得到 200 个代码和结果。
据我所知,我的 cURL 请求是正确的,我完全不知道为什么 API return 使用他们自己建议的示例时关于我的 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);
}
我让你的代码正常工作。我相信您的请求从 API 返回默认 ip-based 响应的两个原因是您在数组上使用 http_build_query 而不是 json_encode,并且因为内容长度在 header 中是 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();
我目前正在开发一个门户网站,以利用 Google 地理位置 API,专门用于 MAC 地址。
我正在使用 PHP 和 cURL 发送对 API 的请求,从 google 解码 returned JSON 并用于测试目的, 呼应结果。
我目前的代码成功向Google的API提交了请求并收到了没有错误的结果。
我遇到的问题是位置结果 returned 从不与 MAC 地址的位置相关,而是始终与用于发送请求的 IP 地址相关。准确率也总是很高(不准确)。
我已经使用建议的 google 输入地址进行了测试:
https://developers.google.com/maps/documentation/geolocation/intro#sample-requests
我可以通过使用 VPN 可靠地更改 return 由 API 编辑的结果。我还确保我的请求包括 'considerIP' 是错误的。如果没有找到地理定位数据,API 应该 return 一个 404 错误,但是我总是得到 200 个代码和结果。
据我所知,我的 cURL 请求是正确的,我完全不知道为什么 API return 使用他们自己建议的示例时关于我的 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);
}
我让你的代码正常工作。我相信您的请求从 API 返回默认 ip-based 响应的两个原因是您在数组上使用 http_build_query 而不是 json_encode,并且因为内容长度在 header 中是 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();