0
I am implementing Google Maps on a real estate site and I have a problem to solve: the request gives error of file_get_contents($url) ...
Right in the middle of the code has the result of the variable $url that if I copy and paste in the browser’s address bar, it returns the perfect file containing all the data of that property with the address shown but the file_get_contents cannot perform the same task.
Does anyone know why? Annex also to url that resumes the error.
Code:
<?php
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
        // CONSULTA GOOGLE MAPS
        $key = "AIzaSyCfYKBdv9RFbsHaRuUu7Pf5Ft8CrQ0wHQo";
        $address = 
            urlencode(
                $linha['ENDERECO_TIPO'].' '.
                $linha['ENDERECO'].', '.
                $linha['NUMERO'].' - Bairro '.
                $linha['BAIRRO'].' - '.
                $linha['CIDADE'].', '.
                $linha['UF'].', 
                Brasil'
        );
        $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".
                $linha['ENDERECO_TIPO'].' '.
                $linha['ENDERECO'].', '.
                $linha['NUMERO'].' - Bairro '.
                $linha['BAIRRO'].' - '.
                $linha['CIDADE'].', '.
                $linha['UF'].', 
                Brasil'     
        ."&sensor=false";
        echo "<strong><em>" .$url. "</em></strong><br /><br />";
        # Este é o resultado da variável $url
        /* http://maps.googleapis.com/maps/api/geocode/json?address=RUA FELICISSIMO DE AZEVEDO, 1 - Bairro HIGIENOPOLIS - PORTO ALEGRE, RS, Brasil&sensor=false */
        $response = file_get_contents($url);
        $response = json_decode($response, true);
        $data['latitude']   = $response['results'][0]['geometry']['location']['lat'];
        $data['longitude']  = $response['results'][0]['geometry']['location']['lng'];
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
?>
Piece of error: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in ...
If you put this code after the first $response and der echo nothing exactly appears: echo "<br /><br />Resposta: " .$response; ... i.e., the file_get_contents either it’s not going or it’s not coming back.
I’m getting the data $date['latitude'] and $date['longitude'] after you have made the modification. Thanks for the help. The map receives the coordinates but does not render. Do you know what might be? http://puu.sh/bU6Iy/43627088d0.png
– Marcos Vinicius
You can’t tell just by looking at the print like that. Try to solve there, but if you can’t, open a separate question to treat this other part, and paste the code as text, not printscreen. But try to figure it out first ;)
– Bacco