Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] Yahoo Weather With Cache System
  • 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.
    define(CACHE_DIR, 'cache');
    define(CACHE_AGE, 3600);
 
    function retrieve($zipCode="92832") {
    $path = CACHE_DIR . '/cache.xml';
    $cached = get_cache_value($path);    
        if(false !== $cached){
            $result = $cached;
        } else {
            $url = "http://weather.yahooapis.com/forecastrss?p=$zipCode&u=c";
            $result = file_get_contents($url);
            
            set_cache_value($path, $result); 
        }
            $XmlObject = new SimpleXMLElement($result);      
            return array('location' =>  $XmlObject->xpath('//yweather:location') ,'forecast' => $XmlObject->xpath('//yweather:forecast'));         
    }
 
    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);
    }