<i id='akhix'><tr id='akhix'><dt id='akhix'><q id='akhix'><span id='akhix'><b id='akhix'><form id='akhix'><ins id='akhix'></ins><ul id='akhix'></ul><sub id='akhix'></sub></form><legend id='akhix'></legend><bdo id='akhix'><pre id='akhix'><center id='akhix'></center></pre></bdo></b><th id='akhix'></th></span></q></dt></tr></i><div id='akhix'><tfoot id='akhix'></tfoot><dl id='akhix'><fieldset id='akhix'></fieldset></dl></div>
    • <bdo id='akhix'></bdo><ul id='akhix'></ul>

        <legend id='akhix'><style id='akhix'><dir id='akhix'><q id='akhix'></q></dir></style></legend>
      1. <small id='akhix'></small><noframes id='akhix'>

      2. <tfoot id='akhix'></tfoot>
      3. 使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP

        时间:2024-05-11

            <tbody id='Hatne'></tbody>

          <small id='Hatne'></small><noframes id='Hatne'>

          <tfoot id='Hatne'></tfoot>
        1. <legend id='Hatne'><style id='Hatne'><dir id='Hatne'><q id='Hatne'></q></dir></style></legend>

            • <i id='Hatne'><tr id='Hatne'><dt id='Hatne'><q id='Hatne'><span id='Hatne'><b id='Hatne'><form id='Hatne'><ins id='Hatne'></ins><ul id='Hatne'></ul><sub id='Hatne'></sub></form><legend id='Hatne'></legend><bdo id='Hatne'><pre id='Hatne'><center id='Hatne'></center></pre></bdo></b><th id='Hatne'></th></span></q></dt></tr></i><div id='Hatne'><tfoot id='Hatne'></tfoot><dl id='Hatne'><fieldset id='Hatne'></fieldset></dl></div>
                <bdo id='Hatne'></bdo><ul id='Hatne'></ul>
                  本文介绍了使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Google 的手机信号塔信息获取纬度和经度地理位置API.它需要一个包含 MCC、MNC、cellId、lac 等信息的有效 JSON,我的 PHP 发布请求如下所示.

                  I'm trying to get latitude and longitude from cell tower info using Google's geolocation api. It requires a valid JSON with information like MCC, MNC, cellId, lac etc.., My PHP post request looks like this.

                  <?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);
                  
                  ?>
                  

                  但是,我在响应中收到错误请求.错误如下所示.

                  However I get an error saying bad request in the response. The error is shown below.

                  Result: {
                   "error": {
                    "errors": [
                     {
                      "domain": "geolocation",
                      "reason": "invalidRequest",
                      "message": "Bad Request"
                     }
                    ],
                    "code": 400,
                    "message": "Bad Request"
                   }
                  }
                  

                  我认为我的 JSON 格式不正确,但它正在执行以下命令行,所以这不是问题.

                  I thought my JSON was not in the correct format but it was working with the following command line execution, so that can't be the issue.

                  $ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY"
                  

                  终端中的上述命令在文件中使用相同的 JSON 正确地给出纬度和经度.我做错了什么?

                  The above command in terminal gives lattitude and longitude properly with the same JSON in a file. What am I doing wrong ?

                  推荐答案

                  试试这个

                      $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 opes padres 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 cabealho 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']);
                  

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

                  Don't forget to create our key on google console.

                  这篇关于使用手机信号塔信息的谷歌地理定位 - Curl 400 Bad request PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:WooCommerce 中特定产品的基于地理位置的自定义重定向 下一篇:在度数和毫秒之间转换

                  相关文章

                      <legend id='82STR'><style id='82STR'><dir id='82STR'><q id='82STR'></q></dir></style></legend>

                      <small id='82STR'></small><noframes id='82STR'>

                      • <bdo id='82STR'></bdo><ul id='82STR'></ul>
                      <i id='82STR'><tr id='82STR'><dt id='82STR'><q id='82STR'><span id='82STR'><b id='82STR'><form id='82STR'><ins id='82STR'></ins><ul id='82STR'></ul><sub id='82STR'></sub></form><legend id='82STR'></legend><bdo id='82STR'><pre id='82STR'><center id='82STR'></center></pre></bdo></b><th id='82STR'></th></span></q></dt></tr></i><div id='82STR'><tfoot id='82STR'></tfoot><dl id='82STR'><fieldset id='82STR'></fieldset></dl></div>

                      <tfoot id='82STR'></tfoot>