php syntax

blossompark
blossompark used Ask the Experts™
on
Hi,
    when i call  the curl.php file in a browser, it displays the location information related to the supply coordinates
 
<?php
$curl_handle=curl_init();
//curl_setopt($curl_handle,CURLOPT_URL,'http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false');
curl_setopt($curl_handle,CURLOPT_URL,'http://maps.google.com/maps/api/geocode/json?latlng=53.290695,-6.273949&sensor=false');
curl_exec($curl_handle);
curl_close($curl_handle);

?>

Open in new window


i now want to supply the coordinates in a query string for example,
http://URL/curl.php?lat=53.123456&lng=-6.123456
however I'm having problems with the syntax in the  line below when i try to embed the variables in the query string...have played around with it but not getting the correct result
curl_setopt($curl_handle,CURLOPT_URL,'http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false');

any help appreciated
<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false');
curl_exec($curl_handle);
curl_close($curl_handle);

?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web Architect
Commented:
you have 2 ways, both below...
<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://maps.google.com/maps/api/geocode/json?latlng='.$lat.','.$lng.'&sensor=false');
curl_exec($curl_handle);
curl_close($curl_handle);

?>

<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false");
curl_exec($curl_handle);
curl_close($curl_handle);

?>

Open in new window

Author

Commented:
hi iobox...excellent!!!
i could have sworn i'd already tried the second option!!!!!
:-)
thanks again

Author

Commented:
just one problem, when i look a little closer the output is  as snippet id 778722

but should be as
{ "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "55 Loreto Ave, Dublin 14, Co. Dun Laoghaire-Rathdown, Ireland", "address_components": [ { "long_name": "55", "short_name": "55", "types": [ "street_number" ] }, { "long_name": "Loreto Ave", "short_name": "Loreto Ave", "types": [ "route" ] }, { "long_name": "Stillorgan", "short_name": "Stillorgan", "types": [ "locality", "political" ] }, { "long_name": "Dun Laoghaire-Rathdown", "short_name": "Dun Laoghaire-Rathdown", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] }, { "long_name": "Dublin 14", "short_name": "Dublin 14", "types": [ "postal_code" ] }, { "long_name": "Dublin 16", "short_name": "Dublin 16", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 53.2901160, "lng": -6.2746120 }, "location_type": "ROOFTOP", "viewport": { "southwest": { "lat": 53.2869684, "lng": -6.2777596 }, "northeast": { "lat": 53.2932636, "lng": -6.2714644 } } } }, { "types": [ "postal_code" ], "formatted_address": "Dublin 14, Co. Dun Laoghaire-Rathdown, Ireland", "address_components": [ { "long_name": "Dublin 14", "short_name": "Dublin 14", "types": [ "postal_code" ] }, { "long_name": "Stillorgan", "short_name": "Stillorgan", "types": [ "locality", "political" ] }, { "long_name": "Dun Laoghaire-Rathdown", "short_name": "Dun Laoghaire-Rathdown", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 53.3128633, "lng": -6.2437449 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 53.2802917, "lng": -6.3127013 }, "northeast": { "lat": 53.3454350, "lng": -6.1747885 } }, "bounds": { "southwest": { "lat": 53.2802917, "lng": -6.3127013 }, "northeast": { "lat": 53.3454350, "lng": -6.1747885 } } } }, { "types": [ "locality", "political" ], "formatted_address": "Stillorgan, Co. Dun Laoghaire-Rathdown, Ireland", "address_components": [ { "long_name": "Stillorgan", "short_name": "Stillorgan", "types": [ "locality", "political" ] }, { "long_name": "Dun Laoghaire-Rathdown", "short_name": "Dun Laoghaire-Rathdown", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 53.2894582, "lng": -6.2006978 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 53.2280819, "lng": -6.2820377 }, "northeast": { "lat": 53.3148845, "lng": -6.1453490 } }, "bounds": { "southwest": { "lat": 53.2280819, "lng": -6.2820377 }, "northeast": { "lat": 53.3148845, "lng": -6.1453490 } } } }, { "types": [ "administrative_area_level_1", "political" ], "formatted_address": "Co. Dun Laoghaire-Rathdown, Ireland", "address_components": [ { "long_name": "Dun Laoghaire-Rathdown", "short_name": "Dun Laoghaire-Rathdown", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 53.2531197, "lng": -6.1631878 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 53.1992829, "lng": -6.2820377 }, "northeast": { "lat": 53.3152830, "lng": -6.0667589 } }, "bounds": { "southwest": { "lat": 53.1992829, "lng": -6.2820377 }, "northeast": { "lat": 53.3152830, "lng": -6.0667589 } } } }, { "types": [ "country", "political" ], "formatted_address": "Ireland", "address_components": [ { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 53.4129100, "lng": -8.2438900 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 51.3066000, "lng": -10.7666000 }, "northeast": { "lat": 55.4602000, "lng": -5.6689000 } }, "bounds": { "southwest": { "lat": 51.3066000, "lng": -10.7666000 }, "northeast": { "lat": 55.4602000, "lng": -5.6689000 } } } } ] }

{ "status": "OK", "results": [ { "types": [ "administrative_area_level_1", "political" ], "formatted_address": "Co. Wicklow, Ireland", "address_components": [ { "long_name": "Wicklow", "short_name": "Wicklow", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 52.9596883, "lng": -6.4028794 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 52.7440709, "lng": -6.9151170 }, "northeast": { "lat": 53.1742358, "lng": -5.8906418 } }, "bounds": { "southwest": { "lat": 52.6820570, "lng": -6.7917990 }, "northeast": { "lat": 53.2338548, "lng": -5.9988317 } } } }, { "types": [ "country", "political" ], "formatted_address": "Ireland", "address_components": [ { "long_name": "Ireland", "short_name": "IE", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 53.4129100, "lng": -8.2438900 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 51.3066000, "lng": -10.7666000 }, "northeast": { "lat": 55.4602000, "lng": -5.6689000 } }, "bounds": { "southwest": { "lat": 51.3066000, "lng": -10.7666000 }, "northeast": { "lat": 55.4602000, "lng": -5.6689000 } } } } ] }

Open in new window

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

ioboxWeb Architect

Commented:
is really hard to notice differences here, can you show us?

Author

Commented:
take the first line of the "good" output from http://www.blossompark.net/google_maps/curl.php?
{ "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "55 Loreto Ave, Dublin 14, Co. Dun Laoghaire-Rathdown, Ireland",

this is the correct address for  "lat": 53.3128633, "lng": -6.2437449
===================================================================

take the first line of the "bad" output from  http://www.blossompark.net/google_maps/curlQueryString.php?lat=53.123456&lng=-6.123456

{ "status": "OK", "results": [ { "types": [ "administrative_area_level_1", "political" ], "formatted_address": "Co. Wicklow, Ireland", "

this is not  the correct address for  "lat": 53.3128633, "lng": -6.2437449
i'm wondering does the  string sent to the google maps api have to be escaped first?

Author

Commented:
Hi iobox.....
my mistake!!!!sorry!!! the lat long  i am supplying is incorrect
lat=53.123456&lng=-6.123456

just noticed now!!!
your code is fine!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial