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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Cache yahoo weather and yahoo currency

    Hello to everyone!
    I have a yahoo weather script and a yahoo currency script in my site but they are taking too much time to load and are slowing my site. Can someone help me to made them caching and refreshing cache every 3600 minutes?

    yahoo weather
    PHP Code:
    <?php
    function retrieveYahooWeather($zipCode="92832") {
        
    $yahooUrl "http://weather.yahooapis.com/forecastrss";
        
    $yahooZip "?p=$zipCode";
        
    $temp "&u=c"// Grade Celsius
        
    $yahooFullUrl $yahooUrl $yahooZip $temp;
        
    $curlObject curl_init();
        
    curl_setopt($curlObject,CURLOPT_URL,$yahooFullUrl);
        
    curl_setopt($curlObject,CURLOPT_HEADER,false);
        
    curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
        
    $returnYahooWeather curl_exec($curlObject);
        
    curl_close($curlObject);
        return 
    $returnYahooWeather;
    }
    $localZipCode "ALXX0002"// Tirane
    $weatherXmlString retrieveYahooWeather($localZipCode);
    $weatherXmlObject = new SimpleXMLElement($weatherXmlString);
    $currentCondition $weatherXmlObject->xpath("//yweather:condition");
    $currentTemperature $currentCondition[0]["temp"];// temperatura
    $currentDescription $currentCondition[0]["text"];// teksti
    $currentImage $currentCondition[0]["code"]; // kodi - perdoret per te marre fotografine/ikonen nga yahoo
    $currentAstronomy$weatherXmlObject->xpath("//yweather:astronomy");
    $currentSunrise $currentAstronomy[0]["sunrise"]; // lindja djellit
    $currentSunset $currentAstronomy[0]["sunset"]; // perendimi djellit
    $currentForecast $weatherXmlObject->xpath("//yweather:forecast");
    $currentHigh $currentForecast[0]["high"]; // temp me e larte e dites
    $currentLow $currentForecast[0]["low"]; // tem me e ulet e dites
    ?>
    * P.S. How to change the time format from ex: 8:21 pm to 20:21?


    yahoo currency
    PHP Code:
    // Funksionet per kembimin valutor

    // Kembimi Euro - Leke
    function kv_euro () {
    $from   'EUR'/*change it to your required currencies */
    $to     'ALL';
    $url 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='$from $to .'=X';
    $handle = @fopen($url'r');

    if (
    $handle) {
        
    $result fgets($handle4096);
        
    fclose($handle);
    }
    $allData explode(',',$result); /* Get all the contents to an array */
    $kveuro $allData[1];
    echo 
    round($kveuro2); // round($dollarValue, 2); - Rrumbullakos shumebn me 2 shifra pas presjes. Ne gjendje normale $dollarValue
    }

    // Kembimi Dollare - Leke
    function kv_dollare () {
    $from   'USD'/*change it to your required currencies */
    $to    'ALL';
    $url 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='$from $to .'=X';
    $handle2 = @fopen($url'r');

    if (
    $handle2) {
        
    $result2 fgets($handle24096);
        
    fclose($handle2);
    }
    $allData2 explode(',',$result2); /* Get all the contents to an array */
    $kvdollare $allData2[1];
    echo 
    round($kvdollare2); // round($dollarValue, 2); - Rrumbullakos shumebn me 2 shifra pas presjes. Ne gjendje normale $dollarValue
    }

    // Kembimi Paund - Leke
    function kv_gbp () {
    $from   'GBP'/*change it to your required currencies */
    $to    'ALL';
    $url 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='$from $to .'=X';
    $handle3 = @fopen($url'r');

    if (
    $handle3) {
        
    $result3 fgets($handle34096);
        
    fclose($handle3);
    }
    $allData3 explode(',',$result3); /* Get all the contents to an array */
    $kvgbp $allData3[1];
    echo 
    round($kvgbp2); // round($dollarValue, 2); - Rrumbullakos shumebn me 2 shifra pas presjes. Ne gjendje normale $dollarValue

    Hope that someone can help me! Thank you in advance!

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Use a database to store the values. Add a timestamp to the data table. Then, when a user visits the page, you check the timestamp and if it is <3600 minutes from the current time you simply display the cached database info. Otherwise grab the data again and overwrite the database entry and update the timestamp.

    Alternatively, create a cron job to run every 3600 minutes and update the database automatically, whether a user visits the page or not.

    To re-format the date use "G" for the hour instead of "g" and you will get the 24 hour format. (http://php.net/manual/en/function.date.php)
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you for your reply, but since i'm new on php/mysql i don;t know how to do it
    Thanks again and waiting for someone that can help me with a code model.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by kleidi View Post
    Thank you for your reply, but since i'm new on php/mysql i don;t know how to do it
    Thanks again and waiting for someone that can help me with a code model.
    Do some Google searching on the tasks mentioned in my last post and you will likely find some tutorials and examples to help you get started. Once you give it a try and show us what you have come up with we can help you get it working.

    Otherwise, you're asking for customized code to be created for you - for free. This is usually something people pay to have done.

    People here will give you free help but generally speaking we won't give you free custom solutions. We require you to build some "sweat equity" in what you are creating.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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