PHP variable question

When I hardcore lat and long in this request all works as expected and there is a JSON result.

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.3403232756246,-77.0765647362513&radius=500&types=&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw

Open in new window


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

Open in new window


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.
seoptiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
We need to see more code.

The following code works
<?php
$lat='38.3403232756246';
$long='-77.0765647362513';
$result = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$lat},{$long}&radius=500&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw";
echo $result;

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
<?php
$lat=38.3403232756246;
$long=-77.0765647362513;
$result = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$lat},{$long}&radius=500&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw";
echo $result;

Open in new window

produces the following
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.340323275625,-77.076564736251&radius=500&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw

Open in new window

Note the truncated values.

Aside: A note on formatting strings - I find embedding variables in strings like so

"This is a {$variable} in the string"

Open in new window

Is more readable and easier to type than
"This is a " . $variable . " in the string"

Open in new window

0
seoptiAuthor Commented:
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.

$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);


.....

Open in new window

0
Ray PaseurCommented:
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:
<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"
        }
    }
}"

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

seoptiAuthor Commented:
Yes $address is defined, I didn't post it.

	$address = $thiStuff['bizAddr'].','.$thisStuff['bizCity'].','.$thisStuff['bizState'].','.$thisStuff['bizZip'];

Open in new window

0
seoptiAuthor Commented:
I think the problem is:

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;

Open in new window



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

Open in new window

0
Ray PaseurCommented:
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.
0
Ray PaseurCommented:
This seems to work correctly.
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);

Open in new window

0
Julian HansenCommented:
There is no difference between

$x = 31.111111;
$x = "This is the latittude : {$lat}";

Open in new window

And

$x = "This is the latitude : 31.111111";

Open in new window


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/
0
seoptiAuthor Commented:
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_results->COORDINATES->latitude.'<br />';

$lat had a <br> assigned. $lat was then used in the API request which broke the whole line.
0
Ray PaseurCommented:
error_reporting(E_ALL) and var_dump() have saved me countless hours!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.