使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP

Posted

技术标签:

【中文标题】使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP【英文标题】:Google geolocation using cell tower info- Curl 400 Bad request PHP 【发布时间】:2015-07-12 12:38:59 【问题描述】:

我正在尝试使用Google's 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 正确地给出纬度和经度。我做错了什么?

【问题讨论】:

【参考方案1】:

试试这个

    $DadosLBS['homeMobileCountryCode'] = $data['MCC'];
    $DadosLBS['homeMobileNetworkCode'] = $data['MNC'];
    $DadosLBS['radioType'] = 'gsm';
    $DadosLBS['carrier'] = $data['MNCOperator'];
    $DadosLBS['cellTowers'] = [
        [
            'mobileCountryCode' => $data['MCC'],
            'mobileNetworkCode' => $data['MNC'],
            'age' => $data['Age'],
            'timingAdvance' => $data['TA'],
            'locationAreaCode' => $data['LAC'],
            'cellId' => $data['CELL_ID'],
            'signalStrength' => $data['SIGNAL'],
        ],
    ];


        //Ver detalhes da API no https://developers.google.com/maps/documentation/geolocation/intro?hl=pt-br

    $service_url = "https://www.googleapis.com/geolocation/v1/geolocate";

    //Chave de acesso
    $Curl_Data = array(
        'key' => <YOUR KEY HERE>
    );

    $CurlQueryString = http_build_query($Curl_Data);

    //Preparando o método a ser enviado os dados
    $Metodo = array(
        CURLOPT_URL         => $service_url.'?'.$CurlQueryString // Define URL to be called
    );

    //Criando s string de dados
    $DadosPost = json_encode($DadosLBS);

    //Preparando as opções padrões do CUrl
    $Curl_Adicional_Options = array(
        CURLOPT_CUSTOMREQUEST       => "POST"
        ,CURLOPT_POSTFIELDS         => $DadosPost
        ,CURLOPT_RETURNTRANSFER     => true              // return web page
        ,CURLOPT_CONNECTTIMEOUT     => 15               // time-out on connect
        ,CURLOPT_TIMEOUT            => 15               // time-out on response
        ,CURLOPT_FAILONERROR        => true             //
        ,CURLOPT_HEADER             => false            // don't return headers
        ,CURLOPT_HTTPHEADER         => array(
                                                'Content-Type: application/json',
                                                'Content-Length: ' . strlen($DadosPost)
                                       ) // Dados para o cabeçalho do post
        ,CURLOPT_FOLLOWLOCATION     => true             // follow redirects
        ,CURLOPT_MAXREDIRS          => 10               // stop after 10 redirects
        ,CURLOPT_SSL_VERIFYPEER     => false
        ,CURLOPT_SSL_VERIFYHOST     => false
    );

    $Curl_Options = array_replace_recursive($Metodo,$Curl_Adicional_Options);

    $cURLConn = curl_init();
    curl_setopt_array($cURLConn, $Curl_Options);

    $vDados['Curl']['Output']       = curl_exec($cURLConn);
    $vDados['Curl']['Error']        = curl_error($cURLConn);
    $vDados['Curl']['ErrorNum']     = curl_errno($cURLConn);
    $vDados['Curl']['ErrorMsg']     = curl_strerror($vDados['Curl']['ErrorNum']);
    $vDados['Curl']['Info']         = curl_getinfo($cURLConn);

    curl_close($cURLConn);

    if ($vDados['Curl']['ErrorNum'] != 0) 
        $Dados['loc'] = array(
            'status' => 'ERROR',
            'error' => array(
                'error_cod' => $vDados['Curl']['ErrorNum'],
                'error_msg' => $vDados['Curl']['ErrorMsg']
            )
        );
        return $Dados['loc'];
    

    //Tratando as respostas
    $vDados['Curl']['Dados'] = json_decode($vDados['Curl']['Output']) or die("Error: Cannot create object");

    print_r($vDados['Curl']['Dados']);

别忘了在谷歌控制台上创建我们的密钥。

【讨论】:

以上是关于使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP的主要内容,如果未能解决你的问题,请参考以下文章

在线地图定位器 api,如 GPS

java中jpeg的地理定位

移动地理定位技术

获取地理位置坐标的来源

如何在信息窗口中显示我的地理编码地址?谷歌地图 API

华为使用h5上传图片没有gps信息