fez Posted January 23, 2012 Share Posted January 23, 2012 Hi, I did post a question earlier but have now perhaps narrowed the problem and am still having trouble! I'm still learning PHP by working on a personal project. I'm having an issue accessing an xml file. If I use the following code I get a 'String could not be parsed as XML' error yet if I remove the $postcode variable when defining the $requestaddress variable and extend the url manually it works! $postcode = $geodata['postcode']; $requestAddress = "http://www.google.com/ig/api?weather=,,," . $postcode; $xml_str = file_get_contents($requestAddress,0); $xml = new SimplexmlElement($xml_str); Any suggestions greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/ Share on other sites More sharing options...
ManiacDan Posted January 23, 2012 Share Posted January 23, 2012 Are you sure the address is correct? Have you printed it out and copy/pasted it into a browser? Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/#findComment-1310417 Share on other sites More sharing options...
fez Posted January 23, 2012 Author Share Posted January 23, 2012 Ooooh. I have just tried this code instead: $postcode = $geodata['town_city']; $requestAddress = "http://www.google.com/ig/api?weather=,,," . $postcode; $xml_str = file_get_contents($requestAddress,0); $xml = new SimplexmlElement($xml_str); And it works! (note the data being retrieved is now the town/city). Although this is great, it is not as accurate as the postcode/zipcode. I suspect this may be because of the space in the postcode? Is there a way to remove the space before declaring? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/#findComment-1310420 Share on other sites More sharing options...
ManiacDan Posted January 23, 2012 Share Posted January 23, 2012 We can't see your screen, we don't see any spaces in any of your variables. Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/#findComment-1310455 Share on other sites More sharing options...
devWhiz Posted January 24, 2012 Share Posted January 24, 2012 You can use str_replace to remove the space from the zipcode... $postalCode = $geodata['postcode']; $postalCode = str_replace(' ','',$postalCode); Hope this helps... -CLUEL3SS Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/#findComment-1310575 Share on other sites More sharing options...
dreamwest Posted January 24, 2012 Share Posted January 24, 2012 $q = urlencode("hollywood cal"); $data = @file_get_contents("http://www.google.com/ig/api?weather={$q}"); $n_data = @new SimpleXmlElement($data, LIBXML_NOCDATA); if($n_data){ $show_img2 = "http://www.google.com{$n_data->weather->current_conditions->icon->attributes()->data}"; $show .= "<div style='float:left;padding:10px;border-right:1px solid grey;width:180px'> <h3>{$n_data->weather->forecast_information->city->attributes()->data} weather</h3><br> <div style='float:left;padding:10px;'> <img title='{$n_data->weather->current_conditions->condition->attributes()->data}' src='{$show_img2}'><br> </div> {$n_data->weather->current_conditions->temp_f->attributes()->data} F | {$n_data->weather->current_conditions->temp_c->attributes()->data} C<br> <nobr>{$n_data->weather->current_conditions->humidity->attributes()->data}</nobr><br> <nobr>{$n_data->weather->current_conditions->wind_condition->attributes()->data}</nobr> </div>"; if(!$n_data->weather->current_conditions->icon->attributes()->data){ $show = ''; } foreach ($n_data->weather->forecast_conditions as $md) { $show_img = "http://www.google.com{$md->icon->attributes()->data}"; $show .= "<div style='float:left;padding:10px'>{$md->day_of_week->attributes()->data}<br> <img title='{$md->condition->attributes()->data}' src='{$show_img}'><br> <span title='Low' style='color:grey;'>{$md->low->attributes()->data}</span> <span title='High'>{$md->high->attributes()->data}</span> </div>"; }//end foreach echo "<div style='padding:10px;min-height:100px'>{$show}</div>"; }//end if array Quote Link to comment https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/#findComment-1310592 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.