Google Maps with file_get_contents error. What can it be?

0

I am implementing Google Maps on a real estate website and I have a problem to solve: the request gives% error of% ...

Soon in the middle of the code is the result of the variable file_get_contents($url) which if I copy and paste it into the address bar of the browser, it returns the perfect file containing all the data of that property with the address presented but the file_get_contents can not perform the same task.

Does anyone know why? Also see url that retakes 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'];
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////

?>

Error: failed to open stream: HTTP request failed! HTTP / 1.0 400 Bad Request in ...

If you put this code after the first $url and $response it appears exactly nothing: echo ... that is, file_get_contents or is not going or is not coming back.     

asked by anonymous 30.09.2014 / 18:15

2 answers

3

A version with cURL follows:

<?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.

    
30.09.2014 / 18:42
1

Maybe your server has file_get_contents blocked. There are two security settings in 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 , you need to enable these settings:

ini_set('allow_url_fopen', true);
ini_set('allow_url_include', true);
    
30.09.2014 / 18:59