Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/255616-string-could-not-be-parsed-as-xml/
Share on other sites

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

 

$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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.