0

I tried using the answer proposed : Get latitude and longitude automatically using php, API

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

but I always get 3 errors

Undefined offset: 0 and Trying to get property of non-object

These errors correspond to the line where there is :

$lat = $response_a->results[0]->geometry->location->lat;
$long = $response_a->results[0]->geometry->location->lng;

Did anyone experience any of this ? I'm using php 5.5

Community
  • 1
  • 1
Bryan
  • 1
  • you sure that's the only code involved? works [fine](http://codepad.viper-7.com/be5brc) – Kevin Aug 26 '14 at 06:57

2 Answers2

0

do something like this

if($response!="")
{
    $response_a = json_decode($response);
    if(isset($response_a->results[0]))
    {
        echo $lat = $response_a->results[0]->geometry->location->lat;
        echo "<br />";
        echo $long = $response_a->results[0]->geometry->location->lng;
    }
    else
    { 
        echo "result is not set in response";
    }   
}
else
{
    echo "No respnse ";
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • Thank SkRocks I've tried your code and I get the following : result is not set in response. It's a problem with json_decode ? You can check my address http://www.awil.ch/test_curl.php – Bryan Aug 26 '14 at 12:13
  • no it means not getting any result for given address – Satish Sharma Aug 27 '14 at 05:07
0

I tested your code on a PHP 5.5.11 machine and it works.

Try to add error_reporting(E_ALL); direct after <?php.

Are you sure the curl module is enabled?

  • Thanks for the answer I have added error_reporting(E_ALL); but it didn't change anything, how can I check if curl module is enabled ? When I do : echo function_exists('curl_version'); I get 1 – Bryan Aug 26 '14 at 07:05
  • Try this: `var_dump(extension_loaded('curl'));` Notice, the requests to Google GeoCoder are limited. Try to open the URL in your browser: http://maps.google.com/maps/api/geocode/json?address=India+Panchkula&sensor=false&region=India – Alexander Aug 26 '14 at 08:12
  • Alexander with var_dum I get TRUE, you can see the running code @ http://www.awil.ch/test_curl.php – Bryan Aug 26 '14 at 12:16
  • Maybe it has to do with the limits from google, but I doubt that I have reached 2,500 requests per 24 hour period or even 10 requests per second. Do you know if there is a way to know where you on your limits levels ? I was looking on google for anything like this but did find it. – Bryan Aug 26 '14 at 13:05
  • I called your code on http://www.awil.ch/test_curl.php and get the position. See screen: http://i58.tinypic.com/1ep3qo.png – Alexander Aug 27 '14 at 06:28