3

I need a function that return me the Timezone of a specific location, so i use the Google Time Zone API.

function timezoneLookup($lat, $lng){

  $url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'&timestamp='.time().'&sensor=false';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $output = curl_exec($ch);
  curl_close($ch);

  return $output;
}     

The function doesn't work because if i return $url i can see that GET variable "&timestamp=" is transformed into "×tamp=".

If i run the script outside the function it works.

WHY??

----UPDATE----

I resolved the problem, the curl doesn't work with https://, so i add:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

See this for more information PHP cURL Not Working with HTTPS

Community
  • 1
  • 1
Maverick
  • 905
  • 8
  • 23
  • are you check in html or in a browser view the url output?? – Emilio Gort Oct 10 '13 at 18:35
  • are you sure that you've passed the 2 function-arguments(that would be the only explanation when it works outside the function, but not from within)? – Dr.Molle Oct 10 '13 at 18:50
  • `×` would produce `×`, but only by a broken/badly build html parser. it shouldn't be happening because `&times` isn't a valid character entity – Marc B Oct 10 '13 at 18:57

2 Answers2

3

The function works fine. The reason you're seeing ×tamp= is because &times is being converted to ×. If you view source you'll see the correct url(instead of viewing the converted entity on the web page).

Why ; is not required

Community
  • 1
  • 1
Galen
  • 29,976
  • 9
  • 71
  • 89
3

There is no problem with this function. If you echo that URL you will get the multiplication sign because it is being filtered through html and recognizing the ascii code. This only happens when you view it though and html viewer (browser), if you view source you will see the original string.

To confirm that this conversion will not occur when passed through curl_setopt(), I ran your code on my server and got an expected result.

echo timezoneLookup(52.2023913, 33.2023913);

function timezoneLookup($lat, $lng){

  $url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'&timestamp='.time().'&sensor=false';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $output = curl_exec($ch);
  curl_close($ch);

  return $output;
}     

Returned...

{ "dstOffset" : 3600, "rawOffset" : 7200, "status" : "OK", "timeZoneId" : "Europe/Kiev", "timeZoneName" : "Eastern European Summer Time" }

If this code is not working for you then it could be a networking issue. Try doing curl with another webpage and see what happens. Also, with a simple api call like this you could easily use file_get_contents()

Kenny
  • 1,543
  • 9
  • 16