Jump to content

variable in an address


paulmo

Recommended Posts

I need to get a php variable or variable value where the zip code code (21619) is:

<?php function getWeather() {$requestAddress = "http://www.google.com/ig/api?weather=21619&hl=en"; etc...

The script works statically with zip code, or with city, state, but not when using a $variable. I need this because my geolocation changes with every user (thanks Scientist). I've tried weather=$city, $state, '$city', '$state', etc. to no avail. Yes $city and $state are working city and state geoloc variables. Thanks in advance.

Link to comment
Share on other sites

can you post the entire code that you are using and indicate where you are getting the zip (dynamically), and where you need it to be placed (in the context of getting a dynamic weather report)

Link to comment
Share on other sites

As requested here is the whole thing. It renders fine with static zip:

function getWeather() {$requestAddress = "http://www.google.com/ig/api?weather=$city, $state"; //THIS IS THE PROBLEM, but works with 04011, new+york, ny etc.$xml_str = file_get_contents($requestAddress,0);// Parses XML $xml = new SimplexmlElement($xml_str);// Loops XML$count = 0;echo '<div id="weather">';foreach($xml->weather as $item) {foreach($item->forecast_conditions as $new) {echo '<div class="weatherIcon">';echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/>';echo $new->day_of_week['data'] . ": ";echo $new->low['data'] . " ";echo $new->high['data'];echo '</div>';}}echo '</div>';}getWeather();?>

Link to comment
Share on other sites

Have you echoed the value of $requestAddress (under real conditions) to make sure it is formatted correctly? Have you checked for non-printing characters in $city and $state ? (I can only guess, since you still have not clarified the origin of those values, despite being asked to do so.) For example, if you're pulling them out of a flat file, you may be pulling newlines or carriage returns along with them.

Link to comment
Share on other sites

This is view source from using static zip code, and weather icons and forecast render on page:

<div id="weather"><div class="weatherIcon"><img src="http://www.google.com//ig/images/weather/cloudy.gif"/>Sat: 24 50</div><div class="weatherIcon"><img src="http://www.google.com//ig/images/weather/sunny.gif"/>Sun: 30 37</div><div class="weatherIcon"><img src="http://www.google.com//ig/images/weather/rain.gif"/>Mon: 42 48</div><div class="weatherIcon"><img src="http://www.google.com//ig/images/weather/rain.gif"/>Tue: 37 54</div></div></div>

Nothing in the <div id="weather"> appears in view source when replacing static zip with this:

weather=$city,$st";

This is $city and $st:

$city =$geoplugin->city;$st =$geoplugin->region;

region is state abbrev. like AZ, ME, NY etc. Using static state abbrev, with city name, also works, like zip code. The geoplugin variables are originally initiated in an included cURL page. I did not post that here because the PHP variables work fine elsewhere. But if it helps, I will post that entire cURL page script. It's long. What I'm after is to have these PHP variables, $city and $st, work in the $requestAddress line where the zip code is. How to do that? I don't know how to check for non-printing characters, but echo $city echo $st renders on page. Thanks in advance.

Link to comment
Share on other sites

How you acquire the data may or may not be important. The first thing is to know if you are in fact getting the data.Have you tried var_dump ($geoplugin) to verify what it contains?EDIT. To clarify, there is nothing obviously wrong with the way you are assembling $requestAddress. If the thing doesn't work, I suspect it is because there is something weird with the values themselves.

Link to comment
Share on other sites

You should be using echo on $requestAddress to see what it looks like (as DD suggested), and your other variables too, just to make sure. big dave also suggested you concatenate your base url and params, i.e. (fleshed out for debugging purposes)

$baseUrl = 'http://www.google.com/ig/api?weather=';echo 'city: ' . $city . '<br>';echo '<br>';echo 'state: ' . $st . '<br>';echo '<br>';$requestAddress = $baseUrl . $city . ',' . $st;echo 'requestAddress: ' . $requestAddress;

Link to comment
Share on other sites

I'm wondering too if your query string contains thisweather=$city,$stor this:weather=$city, $stYou've posted it both ways above. When you type the complete URI in your URI bar in the browser, the space character will be encoded automatically by your browser. If you pass it unencoded to file_get_contents, there might be a problem. (I don't know). Simply eliminating the space seems to work and would require no encoding. So I wonder if you've tried it both ways, and if it fails both ways.Lot's of things to try in a situation like this.

Link to comment
Share on other sites

Scientist's troubleshooting script gives this:

city: Watervillestate: MErequestAddress: http://www.google.com/ig/api?weather=Waterville,ME

DD: I've tried space, no space, +, etc...weather='$city'.'$st'Given the troubleshooting, the problem seems to be the way I'm sticking the PHP variables after weather=, OR this script only works with static input, which I find hard to believe. Also I substituted Scientist's $requestAddress for the original $requestAddress, thinking maybe that would work, but it doesn't.

$baseUrl = 'http://www.google.com/ig/api?weather=';echo 'city: ' . $city . '<br>';echo '<br>';echo 'state: ' . $st . '<br>';echo '<br>';$requestAddress = $baseUrl . $city . ',' . $st;$xml_str = file_get_contents($requestAddress,0); etc...

Link to comment
Share on other sites

what was the original problem?I put http://www.google.com/ig/api?weather=Waterville,ME in the browser and had no problems getting a full XML document. Try it yourself.http://www.google.com/ig/api?weather=Waterville,ME

Link to comment
Share on other sites

You didn't say if you tried the trim function.Are you only looking at the output in the browser window? It might be useful to View Source and see if any linebreaks or other spaces are creeping into the data.You say you've tried this with static data. Do you mean that, or do you mean you have tried it with a string literal? I would test it like this:$city = "Waterville";$state = "ME";$requestAddress = "http://www.google.com/ig/api?weather=$city,$state";That will verify that your string is concatenating correctly (can't see why it wouldn't, though).

Link to comment
Share on other sites

This doesn't work and I get empty div in view source:

$city = "Waterville";$state = "ME";$requestAddress = "http://www.google.com/ig/api?weather=$city,$state";

empty div when trying this as well:

$city =trim($geoplugin->city);$st =trim($geoplugin->region);

The only things that have worked are 1) echoing the variables on their on (not in the http line), and 2) using static info, which in my interpretation just means writing in a zip code or waterville, ME etc. Thanks for continued suggestions.

Link to comment
Share on other sites

That's STATIC input. I need to use PHP variables, and they aren't working as describe above. Thanks.
hey. no need to shout. You made it clear that you were getting $city and $state from geolocate.
Yes $city and $state are working city and state geoloc variables.
My example was given under those pretenses, because you had assured us this was the case. My code example made a url string using these values. Thus, by association, my example was based on dynamic variables (one you said you were getting already). Unless you were substituting string literals without telling us. Also, the fact that we get something using the URL means that perhaps there is something else amiss?What happens if you echo $xml_str (using static or dynamic variables) after using file_get_contents?
Link to comment
Share on other sites

Sorry for shout. No caps. Regarding this:

$requestAddress = "http://www.google.com/ig/api?weather=$city,$st";$xml_str = file_get_contents($requestAddress,0);echo $xml_str;

1) works with weather=zip or city name, state abbrev. 2) does not work with $city and $st variables, defined from geolocation, or trim, ' ' . + etc. 3) does not work with $city = waterville and $st = me

Link to comment
Share on other sites

Sorry for shout. No caps. Regarding this:
$requestAddress = "http://www.google.com/ig/api?weather=$city,$st";$xml_str = file_get_contents($requestAddress,0);echo $xml_str;

1) works with weather=zip or city name, state abbrev. 2) does not work with $city and $st variables, defined from geolocation, or trim, ' ' . + etc. 3) does not work with $city = waterville and $st = me

and not this way either?
$requestAddress = "http://www.google.com/ig/api?weather=" . $city . "," . $st;

Link to comment
Share on other sites

That seems to be getting closer in view source:

<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><problem_cause data=""/></weather></xml_api_reply><div id="weather"></div></div>

Link to comment
Share on other sites

I just threw this together in a test page, and although not technically dynamic in the classic sense, it does seem to resolve issue #3. I see the data when viewing the browsers source.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Test Page</title></head><body><?php$city = "waterville";$state = "ME";$baseUrl = "http://www.google.com/ig/api?weather=";$fullUrl = $baseUrl . $city . "," . $state;echo $fullUrl . "<br/>";$xml_str = file_get_contents($fullUrl,0);echo $xml_str;?></body></html>

and this is what I see when choosing view source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Test Page</title></head><body>http://www.google.com/ig/api?weather=water...br/><?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data="Waterville, ME"/><postal_code data="waterville,ME"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2010-11-21"/><current_date_time data="2010-11-21 18:36:50 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Sunny"/><temp_f data="32"/><temp_c data="0"/><humidity data="Humidity: 25%"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="Wind: N at 8 mph"/></current_conditions><forecast_conditions><day_of_week data="Sun"/><low data="24"/><high data="36"/><icon data="/ig/images/weather/partly_cloudy.gif"/><condition data="Partly Cloudy"/></forecast_conditions><forecast_conditions><day_of_week data="Mon"/><low data="38"/><high data="44"/><icon data="/ig/images/weather/rain.gif"/><condition data="Showers"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="35"/><high data="50"/><icon data="/ig/images/weather/rain.gif"/><condition data="Showers"/></forecast_conditions><forecast_conditions><day_of_week data="Wed"/><low data="26"/><high data="39"/><icon data="/ig/images/weather/partly_cloudy.gif"/><condition data="Partly Cloudy"/></forecast_conditions></weather></xml_api_reply></body></html>
Link to comment
Share on other sites

Thanks for your effort Scientist. But I need these as variables:

$city = $geoplugin->city;$st = $geoplugin->region;

and using the code that you just posted, with my variables above (that do echo on their own), and changing your $state to $st of course, I get this in view source:

http://www.google.com/ig/api?weather=,<br/><?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><problem_cause data=""/></weather></xml_api_reply><div id="weather"></div>

Link to comment
Share on other sites

Thanks for your effort Scientist. But I need these as variables:
$city = $geoplugin->city;$st = $geoplugin->region;

and using the code that you just posted, with my variables above (that do echo on their own), and changing your $state to $st of course, I get this in view source:

http://www.google.com/ig/api?weather=,<br/><?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><problem_cause data=""/></weather></xml_api_reply><div id="weather"></div>

I am aware it wasn't coming from geolocate, but I just wanted to see if this would at least solve one of the problems, on our way to solving the whole thing. Something is wrong in the request because it obviously not returning anything. If you could get it to work using string literals as the value, then we could pin it down to how PHP is interpreting those goelocate variables.
Link to comment
Share on other sites

After some patience I just did get it to work with string literals (again) , if you mean "Waterville" and "ME." But then I tried $city and $st again and nothing. You're right: the baseUrl or requestAddress line is not working with variables:

$fullUrl = $baseUrl . $city . "," . $st;

I think it might be a syntax issue.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...