Google geolocation using cell tower info- Curl 400 Bad request PHP

前端 未结 1 1188
长发绾君心
长发绾君心 2021-01-07 04:34

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.., M

相关标签:
1条回答
  • 2021-01-07 05:26

    Try this

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

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

    0 讨论(0)
提交回复
热议问题