seopti
asked on
PHP variable question
When I hardcore lat and long in this request all works as expected and there is a JSON result.
But when I place $lat and $long in a variable and do this request:
There seems to be no output. I can't quite understand why because when I echo $lat and $long the result for $lat and $long is there.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.3403232756246,-77.0765647362513&radius=500&types=&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw
But when I place $lat and $long in a variable and do this request:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=".$lat.",".$long."&radius=500&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw
There seems to be no output. I can't quite understand why because when I echo $lat and $long the result for $lat and $long is there.
ASKER
Thank you for the detailed answer. I tried the solution you posted but no success. It always only works with the hardcoded lat and long not with $lat and $long.
This is the code which works fine but only with hardcoded values.
This is the code which works fine but only with hardcoded values.
$address_url = "http://www.geocodefarm.com/api/forward/json/c3140ba389a289bd42c51ebe2d72ccc67fb4633c/$address";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $address_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);
//Print the provided address in Human readable/ complete Postal address;
echo $address_returned = $response_a->geocoding_results->ADDRESS->address_returned.'<br />';
//Print the Latitude and Longitude of the address
echo $lat = $response_a->geocoding_results->COORDINATES->latitude.'<br />';
echo $long = $response_a->geocoding_results->COORDINATES->longitude;
$api_key = "AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw";
foreach ($categoryArray as $category => $title) { ?>
<div class="<?php echo (array_key_exists($category, $loadedCatArray) ? "" : "hidden"); ?>" id="<?php echo $category; ?>_items">
<div class="data_title"><?php echo $title; ?></div>
<ol class="data_items">
<?php
$bizArray = file_get_contents("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.3403232277480,-77.0765647362513&radius=500&types=".$category."&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw");
$bizArray = json_decode($bizArray, true);
.....
In the most recent code snippet on line 1, $address is an undefined variable. Are we maybe still missing some of the code?
When I fix the parse errors and dump the response from the API, this is what I see:
When I fix the parse errors and dump the response from the API, this is what I see:
<b>Notice</b>: Undefined variable: address in <b>/home/iconoun/public_html/demo/temp_seopti.php</b> on line <b>4</b><br />
string(1202) "{
"geocoding_results": {
"LEGAL_COPYRIGHT": {
"copyright_notice": "Copyright (c) 2013 Grabenhofen Corporation (Colorado, USA) - All Rights Reserved.",
"result_distribution": "UNPERMITTED, UNLICENSED",
"copyright_logo": "https:\/\/www.geocodefarm.com\/assets\/img\/logo.png",
"terms_of_service": "https:\/\/www.geocodefarm.com\/policies\/terms-of-service\/",
"privacy_policy": "https:\/\/www.geocodefarm.com\/policies\/privacy-policy\/"
},
"STATUS": {
"access": "KEY_VALID, ACCESS_GRANTED",
"status": "FAILED, NO_RESULTS"
},
"ACCOUNT": {
"name": "Thomas Kowalewski",
"email": "support@apollofind.com",
"api_key": "c3140ba389a289bd42c51ebe2d72ccc67fb4633c",
"monthly_due": "75.00",
"next_due": "2014-11-25 23:06:13",
"distribution_license": "NONE, UNLICENSED",
"usage_limit": "100000",
"used_today": "1329",
"remaining_queries": "98671"
},
"STATISTICS": {
"load_time": "0.41",
"https_ssl": "DISABLED, INSECURE"
}
}
}"
ASKER
Yes $address is defined, I didn't post it.
$address = $thiStuff['bizAddr'].','.$thisStuff['bizCity'].','.$thisStuff['bizState'].','.$thisStuff['bizZip'];
ASKER
I think the problem is:
This will echo $lat and $long after a successful geocoding without any problems.
But when $lat and $long is used in this API Call it will not.
This will echo $lat and $long after a successful geocoding without any problems.
echo $lat = $response_a->geocoding_results->COORDINATES->latitude.'<br />';
echo $long = $response_a->geocoding_results->COORDINATES->longitude;
But when $lat and $long is used in this API Call it will not.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$lat},{$long}&radius=500&types=".$category."&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw
I think the problem is that we do not know what variables you have in that script, and neither do you!
Here's a good way to begin to understand the inner workings. This function is your friend. Use it every time you are not 100% sure of the contents of any variable - especially variables from external sources like $_GET and $_POST.
http://php.net/manual/en/function.var-dump.php
When you use var_dump() on a variable that may contain HTML or other markup, you may need to use your browser's "view source" to see the true contents of the variable.
Here's a good way to begin to understand the inner workings. This function is your friend. Use it every time you are not 100% sure of the contents of any variable - especially variables from external sources like $_GET and $_POST.
http://php.net/manual/en/function.var-dump.php
When you use var_dump() on a variable that may contain HTML or other markup, you may need to use your browser's "view source" to see the true contents of the variable.
This seems to work correctly.
http://iconoun.com/demo/temp_seopti.php
http://iconoun.com/demo/temp_seopti.php
<?php // demo/temp_seopti.php
// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28545160.html
// USE THIS SETTING TO BE SURE YOU'RE NOT ACCIDENTALLY RELYING ON AN UNDEFINED VARIABLE
error_reporting(E_ALL);
// ASSIGN VALUES TO VARIABLES
$lat = '38.930451';
$long = '-77.148134';
// CREATE THE URL, USING THE VARIABLES WE JUST ASSIGNED
$url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=500&types=&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw";
// FETCH THE RESPONSE AND DISPLAY THE DATA
$doc = file_get_contents($url);
echo '<pre>';
var_dump($url);
var_dump($doc);
There is no difference between
There is something you are not showing us.
Can't see where in your code snippet you are using the string you say is not working/
$x = 31.111111;
$x = "This is the latittude : {$lat}";
And $x = "This is the latitude : 31.111111";
There is something you are not showing us.
Can't see where in your code snippet you are using the string you say is not working/
ASKER
I'm so dumb, found out the error thanks to Ray, I had error reportung turned off before.
The problem was with this variable:
$lat = $response_a->geocoding_res ults->COOR DINATES->l atitude.'< br />';
$lat had a <br> assigned. $lat was then used in the API request which broke the whole line.
The problem was with this variable:
$lat = $response_a->geocoding_res
$lat had a <br> assigned. $lat was then used in the API request which broke the whole line.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following code works
Open in new window
Produces the required result.If you are using numeric values for $lat / $lang you might get a truncated answer - for instance
Open in new window
produces the followingOpen in new window
Note the truncated values.Aside: A note on formatting strings - I find embedding variables in strings like so
Open in new window
Is more readable and easier to type thanOpen in new window