Solved

PHP variable question

Posted on 2014-10-27
11
109 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 54

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 109

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
LVL 58

Expert Comment

by:Gary
ID: 40406600
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 109

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 109

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 54

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 109

Accepted Solution

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

786 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