Google Maps with file_get_contents error. What can it be?

Asked

Viewed 905 times

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.

2 answers

3


Here is a version with Curl:

<?php

    $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='.$address;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);

    $response = json_decode( $response, true );

    $data['latitude']   = $response['results'][0]['geometry']['location']['lat'];
    $data['longitude']  = $response['results'][0]['geometry']['location']['lng'];

    echo 'Lat:'.$data['latitude'].' - Lon:'.$data['longitude'];
?>


Note that $key is not being used in this example.

  • 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

  • 1

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

1

Maybe your server is with the file_get_contents blocked. There are two security settings on php.ini that block the use of file_get_contents.

They are:

allow_url_fopen and allow_url_include

Make sure they are disabled using the following snippet in your code:

<?php
var_dump(ini_get('allow_url_fopen'));
var_dump(ini_get('allow_url_include'));
?>

If the result is false, need to enable these settings :

ini_set('allow_url_fopen', true);
ini_set('allow_url_include', true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.