Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php error Appears after 10min

    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...m,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 Code:
    <?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>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The cause is a non-responsive request to an external server.
    Simply check if $xml is false before doing anything else:
    PHP Code:
    <?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.';
    }
    ?>

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,306
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,306
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by sven_paul View Post
    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.

    Quote Originally Posted by sven_paul View Post
    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.

    Quote Originally Posted by sven_paul View Post
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •