Jump to content

URL in variable


fez

Recommended Posts

Hi,

 

I'm still pretty new to PHP and am currently working on a personal project to help learn. I am trying to detect a user's location, store their city and region and then build a URL containing these for later use.

 

<?php 
$city = "<script language='javascript'>document.write(geoip_city());</script>"; 
$county = "<script language='javascript'>document.write(geoip_region_name());</script>"; 

$geourl = "http://www.google.com/ig/api?weather=,,," . $city . "," . $county;

?>

 

If I echo $geourl then it displays just fine but when I attempt to use it, it throws an error 'String could not be parsed as XML' .

 

$xml_str = file_get_contents($geourl,0);

 

Any help appreciated, I just can't get my head around it!

 

Fez

Link to comment
https://forums.phpfreaks.com/topic/255583-url-in-variable/
Share on other sites

Thanks, Thorpe.

 

It actully does appear to be the line below that is causing the error (  $xml = new SimplexmlElement($xml_str);  )

 

Here's the full code.

 

<?php 
$city = "<script language='javascript'>document.write(geoip_city());</script>"; 
$county = "<script language='javascript'>document.write(geoip_region_name());</script>"; 

$geourl = "http://www.google.com/ig/api?weather=,,," . $city . "," . $county;

function getWeather() {
$xml_str = file_get_contents($geourl,0);
$xml = new SimplexmlElement($xml_str);
$count = 0;
echo '<div id="weather">';
     foreach($xml->weather as $item) {
          foreach($item->current_conditions as $new) {
               echo $new->condition['data'];
               echo '<br/>';
               echo $new->temp_c['data'];
               echo '°C';
               echo '<br/>';
               echo $new->wind_condition['data'];

               }
          }
     echo '</div>';
}
getWeather();
?>

Link to comment
https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310266
Share on other sites

Hmmm, yes I think you're right.

 

When I use this code it works fine:

 

$geourl = "http://www.google.com/ig/api?weather=,,,bristol,avon";

 

Yet, when I attempt to use the variables like before:

 

$geourl = "http://www.google.com/ig/api?weather=,,," . $city . "," . $county;

 

It doesn't work! What is different? Any ideas?

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310279
Share on other sites

Thanks.

 

I tried what you said and it now throws this error:

 

Warning: file_get_contents(http://www.google.com/ig/api?weather=,,,Bristol,Avon): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in - on line 64 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in -:65 Stack trace: #0 -(65): SimpleXMLElement->__construct('') #1 -(89): getWeather('http://www.goog...') #2 {main} thrown in - on line 65

 

(  line 64 is this one: $xml_str = file_get_contents($geourl,0);  )

 

This is taking me way longer than I though it would  :wtf::o

Link to comment
https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310326
Share on other sites

The following code seems to work for me:

 

<?php 
error_reporting(E_ALL);
ini_set('display_errors',1);


$city = 'bristol';
$county = 'avon';
$geourl = "http://www.google.com/ig/api?weather=,,," . $city . "," . $county;
$xml_str = file_get_contents($geourl,0);

function getWeather($geourl) {
$xml_str = file_get_contents($geourl,0);
var_dump($xml_str);
}

getWeather($geourl);
?>

 

 

Looking at the code you posted earlier, how are you getting the value for $city and $county? It looks like you're just assigning some JavaScript code to the variables. As far as I'm aware, JavaScript and PHP don't play well together since one is a client-side language while the other is server-side.

 

What happens if you dump $geourl below?

 

<?php
$city = "<script language='javascript'>document.write(geoip_city());</script>";
$county = "<script language='javascript'>document.write(geoip_region_name());</script>";
$geourl = "http://www.google.com/ig/api?weather=,,," . $city . "," . $county;

var_dump($geourl);

//...
?>

Link to comment
https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310616
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.