我正在尝试使用Google 的 geolocation api从手机信号塔信息中获取纬度和经度。它需要一个包含 MCC、MNC、cellId、lac 等信息的有效 JSON,我的 PHP 发布请求如下所示。
<?php
header("Access-Control-Allow-Origin: *");
$mcc = $_POST["mcc"];
$mnc = $_POST["mnc"];
$cellId = $_POST["cellId"];
$lac = $_POST["lac"];
$post_array = array(
"cellId" => (int) $cellId,
"locationAreaCode" => (int) $lac,
"mobileCountryCode" => (int) $mcc,
"mobileNetworkCode" => (int) $mnc,
);
$post_data = json_encode(array('cellTowers' => array($post_array)));
echo $post_data;
$url = "https://www.googleapis.com/geolocation/v1/geolocate?key=".$api_key; // not including api key here but its there in my code
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => $post_data
));
$result = curl_exec($ch);
echo "Result: ".$result;
curl_close($ch);
?>
但是,我在响应中收到错误请求。错误如下所示。
Result: {
"error": {
"errors": [
{
"domain": "geolocation",
"reason": "invalidRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
我认为我的 JSON 格式不正确,但它正在使用以下命令行执行,所以这不是问题。
$ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY"
终端中的上述命令使用文件中的相同 JSON 正确提供纬度和经度。我究竟做错了什么 ?