Uregelmæssig fejl med SimpleXMLElement
Jeg forsøger at trække nogle vejr-oplysninger ud af Googles API vha. xml. Dertil har jeg fundet et script på nettet der ser således ud:---
<?php
$ch = curl_init();
$timeout = 0;
//Set CURL options
curl_setopt ($ch, CURLOPT_URL, 'http://www.google.com/ig/api?weather=pothia,greece&oe=utf-8&hl=da');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$xml_str1 = curl_exec($ch);
//close CURL cause we dont need it anymore
curl_close($ch);
// Parse the XML response
$xml = new SimplexmlElement($xml_str);
foreach($xml->weather as $item) {
foreach($item->current_conditions as $new) {
//For temperature in fahrenheit replace temp_c by temp_f
$current_temperature=$new->temp_c['data'];
$current_humidity=$new->humidity['data'];
$current_wind=$new->wind_condition['data'];
$current_icon=$new->icon['data'];
}
$current_condition=$item->forecast_conditions[0]->condition['data'];
$next_temperature=$item->forecast_conditions[1]->high['data'];
//to convert Fahrenheit into Celcius
$next_temperature=round(($next_temperature-32)*(5/9));
$next_condition=$item->forecast_conditions[1]->condition['data'];
}
$weather_icon = "http://www.google.com$current_icon";
?>
---
Det virker for det meste OK, men nogle gange får jeg denne fejl når siden reloades:
---
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xA0 0x25 0x22 0x2F in C:\XAMMP\xampp\htdocs\Google\weather_inc.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ata="Klart"/><temp_f data="77"/><temp_c data="25"/><humidity data="Fugtighed: 78 in C:\XAMMP\xampp\htdocs\Google\weather_inc.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\XAMMP\xampp\htdocs\Google\weather_inc.php on line 17
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\XAMMP\xampp\htdocs\Google\weather_inc.php:17 Stack trace: #0 C:\XAMMP\xampp\htdocs\Google\weather_inc.php(17): SimpleXMLElement->__construct('<?xml version="...') #1 C:\XAMMP\xampp\htdocs\Google\Google-rejsetips-a-h.php(1): include('C:\XAMMP\xampp\...') #2 {main} thrown in C:\XAMMP\xampp\htdocs\Google\weather_inc.php on line 17
---
Jeg er ikke super stærk til hverken PHP eller XML, så jeg kan ikke helt gennemskue hvorfor problemet opstår (og hvorfor det kun er nogle gange).
Nogen der kan hjælpe?