Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML entity '&times' and php function

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

like image 608
Maverick Avatar asked Mar 03 '26 12:03

Maverick


2 Answers

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

like image 93
Galen Avatar answered Mar 06 '26 01:03

Galen


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

like image 32
KHMKShore Avatar answered Mar 06 '26 01:03

KHMKShore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!