Solved

PHP variable question

Posted on 2014-10-27
11
96 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:seopti
Comment Utility
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
Comment Utility
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:seopti
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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 108

Accepted Solution

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it 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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now