Have an account? Sign in
Login  Register  Facebook
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
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

// 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($handle, 4096);
    fclose($handle);
}
$allData = explode(',',$result); /* Get all the contents to an array */
$kveuro = $allData[1];
echo round($kveuro, 2); // 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($handle2, 4096);
    fclose($handle2);
}
$allData2 = explode(',',$result2); /* Get all the contents to an array */
$kvdollare = $allData2[1];
echo round($kvdollare, 2); // 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($handle3, 4096);
    fclose($handle3);
}
$allData3 = explode(',',$result3); /* Get all the contents to an array */
$kvgbp = $allData3[1];
echo round($kvgbp, 2); // round($dollarValue, 2); - Rrumbullakos shumebn me 2 shifra pas presjes. Ne gjendje normale $dollarValue
}

Hope that someone can help me! Thank you in advance!
Started: September 18, 2011 Latest Activity: September 18, 2011 cache yahoo weather currency script
3 Answers
for the yahoo weather just use the two cache functions with:
function retrieveYahooWeather($zipCode="92832") {
    $path = CACHE_DIR . '/cache.xml';
    $cached = get_cache_value($path);    
    if(false !== $cached){
        echo "\nFROM CACHE\n";
        return $cached;
    } else {
       echo "\nFROM LIVE\n";
       $url = "http://weather.yahooapis.com/forecastrss?p=$zipCode&u=c";
       $result = file_get_contents($url);
       
       set_cache_value($path, $result);
       return $result;
    }
}

Posted: MacOS
In: September 18, 2011

Thank you very much. It works now and my site is loading faster. ;)
September 18, 2011

The idea is:
  • create some sort of cache dir and set the defined cache age
  • then, at the very beginning of your function, check for cache if it exists, check it\'s age If within range, get it if cache too old use live data and set that data into cache file.
Something like this should do the trick:
define(CACHE_DIR, \'E:/xampp/xampp/htdocs/tmp\');
define(CACHE_AGE, 3600);
/**
 * Adds data to the cache, if the cache key doesn\'t aleady exist.
 * @param string $path the path to cache file (not dir)
 * @return false if there is no cache file or the cache file is older that CACHE_AGE. It return cache data if file exists and within CACHE_AGE 
 */
function get_cache_value($path){
    if(file_exists($path)){
        $now = time();
        $file_age = filemtime($path);
        if(($now - $file_age) < CACHE_AGE){
            return file_get_contents($path);
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function set_cache_value($path, $value){
    return file_put_contents($path, $value);
}
and
function kv_euro () {
    $path = CACHE_DIR . \'/euro.txt\';

    $kveuro = get_cache_value($path);
    if(false !== $kveuro){
        echo \"nFROM CACHEn\";
        return round($kveuro, 2);
    } else {
        echo \"nFROM LIVEn\";
        $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($handle, 4096);
            fclose($handle);
        }
        $allData = explode(\',\',$result); /* Get all the contents to an array */
        $kveuro = $allData[1];
        set_cache_value($path, $kveuro);
        return $kveuro;
    }
}

Posted: MacOS
In: September 18, 2011

Thank you bro ... the currency cache works perfectly thanks to you. Now, i tried to work at the same way with yahoo weather but i had no success. Can you help me on this one too, PLEASE? Thanks again ;)!
September 18, 2011

You need some place to store these results. MySQL is a popular choice, but if the data does not need to stick around or have historical values, using memcache would be easier. Depending on your host, both of these options may be available.

Posted: Go
In: September 18, 2011

Thank you for your reply and helping me by, since i'm new on php/mysql i don't know how to do it :( Thanks again!
September 18, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer