fez Posted January 23, 2012 Share Posted January 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/ Share on other sites More sharing options...
trq Posted January 23, 2012 Share Posted January 23, 2012 Are you sure it is that line that is throwing the error? What are you doing with $xml_string after this? Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310265 Share on other sites More sharing options...
fez Posted January 23, 2012 Author Share Posted January 23, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310266 Share on other sites More sharing options...
trq Posted January 23, 2012 Share Posted January 23, 2012 That would imply that $xml_str is not valid xml. Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310270 Share on other sites More sharing options...
fez Posted January 23, 2012 Author Share Posted January 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310279 Share on other sites More sharing options...
cyberRobot Posted January 23, 2012 Share Posted January 23, 2012 It's probably an issue with variable scope. Have you tried passing $geourl to getWeather(). Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310297 Share on other sites More sharing options...
fez Posted January 23, 2012 Author Share Posted January 23, 2012 How would I successfully do this? Like I said, I'm new here! I really do appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310308 Share on other sites More sharing options...
cyberRobot Posted January 23, 2012 Share Posted January 23, 2012 How would I successfully do this? Like I said, I'm new here! I really do appreciate the help. More information about function arguments can be found here: http://www.php.net/manual/en/functions.arguments.php Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310310 Share on other sites More sharing options...
cyberRobot Posted January 23, 2012 Share Posted January 23, 2012 Basically, your code would be modified to: <?php //... function getWeather($geourl) { //... } getWeather($geourl); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310311 Share on other sites More sharing options...
fez Posted January 23, 2012 Author Share Posted January 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310326 Share on other sites More sharing options...
cyberRobot Posted January 24, 2012 Share Posted January 24, 2012 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); //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/255583-url-in-variable/#findComment-1310616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.