0

I've this php script that loads google weather from google API.

<?php

      //Weather Forecast icon and temperature from google weather API

      $setYourLanguage = "en"; // Possibilities: "en" - english; "ru" - russian; "ka" - georgian;
      //WEATHER, read google XML weather forecast
      $URL = "http://www.google.com/ig/api?weather=".$myCity."&hl=".$setYourLanguage."";

      $dataInISO = file_get_contents($URL);
      $dataInUTF = mb_convert_encoding($dataInISO, "UTF-8", "ISO-8859-2"); //fix Google's API UTF-8 bug
      $xml = simplexml_load_string($dataInUTF); 

      $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
      $iconData = str_replace("/ig/images/weather/", "weather/",  $current[0]->icon['data']);
      $iconData = str_replace(".gif", ".png", $iconData);

?>

But sometimes page doesn't load and I get error in apache2 logs:

[Thu Aug 23 12:04:16 2012] [error] [client ::1] PHP Warning:  simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/homepage-structure.php on line 23
[Thu Aug 23 12:04:16 2012] [error] [client ::1] PHP Warning:  simplexml_load_string(): Unsupported API in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/homepage-structure.php on line 23
[Thu Aug 23 12:04:16 2012] [error] [client ::1] PHP Warning:  simplexml_load_string(): ^ in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/homepage-structure.php on line 23
[Thu Aug 23 12:04:16 2012] [error] [client ::1] PHP Fatal error:  Call to a member function xpath() on a non-object in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/homepage-structure.php on line 25
[Thu Aug 23 12:05:57 2012] [error] [client ::1] What

Any ideas why this is happening? My theory is that it cannot connect correctly to URL and fails in this way... but why is page loading stopped? Is there any way to avoid this error? and allow page to load fully?

1
  • Works fine for me. Could you provide var_dump($dataInISO)? Commented Aug 23, 2012 at 8:17

1 Answer 1

1

If SimpleXml fails to load the XML, the value of the variable $xml is false and not an instance of type SimpleXMLElement. That is, what the error message said:

[Thu Aug 23 12:04:16 2012] [error] [client ::1] PHP Fatal error: Call to a member function xpath() on a non-object in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/homepage-structure.php on line 25

So you should check against that before accessing a function on the object.

if ($xml) {
      $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
      $iconData = str_replace("/ig/images/weather/", "weather/",  $current[0]->icon['data']);
      $iconData = str_replace(".gif", ".png", $iconData);
}

Then your page will load completely, even if the XML load from the webservice fails.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.