...

View Full Version : Php error Appears after 10min



sven_paul
04-12-2012, 03:42 PM
hi, im trying to use the google weather api. Everything worked in 10 min. after 10 min i started to get this error.
And i have no idea what caused it, i tried to upload the script again but still same error.


Warning: simplexml_load_file(http://www.google.com/ig/api?weather=stockholm,sweden&hl=en) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in /home/www/XXXXXX.com/lab/weather/load_weather.php on line 2

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.google.com/ig/api?weather=stockholm,sweden&hl=en" in /home/www/XXXXXX.com/lab/weather/load_weather.php on line 2

Fatal error: Call to a member function xpath() on a non-object in /home/www/XXXXX.com/lab/weather/load_weather.php on line 3


<?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=stockholm,sweden&hl=en');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<div class="weather">
<img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
<span class="condition"></br>
<?= $current[0]->temp_c['data'] ?> </br>
<?= $current[0]->condition['data'] ?></br>
<?= $current[0]->humidity['data'] ?></br>
<?= $current[0]->wind_condition['data'] ?></br>
<? date_default_timezone_set('Europe/Stockholm');
echo date('G:i:s');
?>
</span>
</div>

Fou-Lu
04-12-2012, 04:05 PM
The cause is a non-responsive request to an external server.
Simply check if $xml is false before doing anything else:


<?php

if (false !== ($xml = @simplexml_load_file('http://www.google.com/ig/api?weather=stockholm,sweden&hl=en')))
{
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<div class="weather">
<img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
<span class="condition"></br>
<?= $current[0]->temp_c['data'] ?> </br>
<?= $current[0]->condition['data'] ?></br>
<?= $current[0]->humidity['data'] ?></br>
<?= $current[0]->wind_condition['data'] ?></br>
<? date_default_timezone_set('Europe/Stockholm');
echo date('G:i:s');
?>
</span>
</div>
<?php
}
else
{
print 'Could not load weather data.';
}
?>

sven_paul
04-12-2012, 04:10 PM
Hey, thanks for the reply,
'Could not load weather data.' is now printed. Can i solve the problem, is strange cause it worked 30 min ago?!.


Best regards

Fou-Lu
04-12-2012, 04:15 PM
Nope. Cause of 503 indicates the problem is on the server end, not the client end.
What you can do is use a db or file of your own to write this data too. If the service is unavailable, you can serve from file. You should only really be updating once every half hour or so anyway.

tangoforce
04-12-2012, 05:44 PM
As Fou is suggesting, you really should cache the data. The weather in the selected area isn't going to change drastically within 10 minutes. If clouds start heading towards you then you'll see them first. That and the weather sites don't update that frequently anyway - do you really think they update every few seconds? - If they do my widgets on my desktop certainly don't pick it up!

Just cache the data and make a new request every 5-10 minutes. Thats more than plenty. The 503 error may be google directly blocking your requests due to excessive requests.

sven_paul
04-13-2012, 01:50 PM
Ahh, ok. so the errors appears when i do the call every second.
Yes I thought that it was realtime, the point of doing it in realtime is that the collected data will affect a animation, so id hope there would be constant weather changes, which woud generate a smooth animation.

how do I cache it?

tangoforce
04-13-2012, 03:27 PM
Ahh, ok. so the errors appears when i do the call every second.

No 100% answer there BUT probably yes. Google may well be automatically issuing you 503 errors when it detects you making so many requests.



Yes I thought that it was realtime, the point of doing it in realtime is that the collected data will affect a animation, so id hope there would be constant weather changes, which woud generate a smooth animation.

Weather is real time yes however sites like google will still collect data from their sources every few minutes rather than drain the sources bandwidth relentlessly. While your animation may seem like a great idea it may not be possible unless you can obtain a real time data source that will let you hit them relentlessly draining their bandwidth.


how do I cache it?

Store it somewhere - file, database but not the session (that will be different for each user). Then just read from that until your next call to the weather feed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum