-1

I'm working in to get the latitude and longitude from google maps through the geolocation API. I have tried many times but always is showing this error:

Trying to get property of non-object in C:\wamp64\www\index3.php on line 16

The issue is that I'm not able to get the info from the $url so then, cannot get the latitude and longitude.

I changed my code and now it is, but still with no working.

<php

$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=*************************************";


$json = file_get_contents($url); // I tried with the @ symbol and without it

$data=json_decode($json);

echo ($data->results[0]->geometry->location->lat);

?>

And also I used this one:

<php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";

$json = file_get_contents($url); // I tried with the @ symbol and without it
$data = json_decode($json);

echo "Latitud".($data->results[0]->geometry->location->lat);

?>

Without the @ symbol I have this error line

Warning: file_get_contents() has been disabled for security reasons in [...][...] on line 8

6 Answers6

0

change http to https in your request url.

Abrucius
  • 101
  • 6
0

As Abrucius, you msut use HTTPS or google will deny the request. You must also encode the address or else you will get a "Bad request" error.

<?php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";

$json = file_get_contents($url);
$data = json_decode($json);

echo ($data->results[0]->geometry->location->lat);    

Outputs: 12.9082396

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
  • Yeah I dit it, but it stills showing the same error... in the last one line – Daniel Sandoval Oct 24 '17 at 17:40
  • Next time may I suggest starting with such an obvious error in your OP? The error states specifically that the function has been disabled on a server level. Try using `curl` but if `file_get_contents` has been disabled, then it's likely curl has aswell. Find a different host.. – IsThisJavascript Oct 26 '17 at 09:52
0

replace following code

    $address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";

to

 $address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";
romal tandel
  • 481
  • 12
  • 19
0

You have provided http in the url, it should be https. Try below code that will help you. You have to use urlencode() because api will not allow space in the address, so you have to encode.

$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$address = urlencode($address);

$url = "https://maps.google.com/maps/api/geocode/json?address={$address}&key=***************************";

$resp_json = file_get_contents($url);

$resp = json_decode($resp_json, true);
echo "<pre>";print_r($resp["results"][0]['geometry']['location']['lat']);

Try this code will work for sure

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
  • Did it, but it doesn't work. Even I removed the "at" symbol from the `file_get_contents` instruction, and shows me this: `file_get_contents() has been disabled for security reasons` – Daniel Sandoval Oct 24 '17 at 17:45
  • `Warning: file_get_contents() has been disabled for security reasons in [...][...]` you are getting error. Then you have to contact server provider to enable that – Pankaj Makwana Oct 25 '17 at 05:14
0

WillParky93's answer seems to work fine for me. Maybe try following the next link to allow https wrappers:

How to get file_get_contents() to work with HTTPS?

...or post the error you're getting so we can help you out

diegales
  • 25
  • 5
0

I just solved this issue, I was getting this error because of a proxy configuration. I was testing into a network with a proxy configuration. So, the proxy is not allowing me to connect with the Google database. Then I tried to connect with a different network and it worked perfectly. Thank you guys for your aid!