Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP variable question

Posted on 2014-10-27
11
Medium Priority
?
137 Views
Last Modified: 2014-10-27
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.
0
Comment
Question by:seopti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 40406524
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
 

Author Comment

by:seopti
ID: 40406567
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40406585
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:seopti
ID: 40406597
Yes $address is defined, I didn't post it.

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

Open in new window

0
 

Author Comment

by:seopti
ID: 40406614
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40406625
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40406639
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 40406675
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
 

Author Comment

by:seopti
ID: 40407097
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40407108
error_reporting(E_ALL) and var_dump() have saved me countless hours!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question