示例#1
0
 /**
  * Retrieves data from cache, if it's there.  If it is, but it's expired,
  * it performs a conditional GET to see if the data is updated.  If it
  * isn't, it down updates the modification time of the cache file and
  * returns the data.  If the cache is not there, or the remote file has been
  * modified, it is downloaded and cached.
  *
  * @param string URL of remote file to retrieve
  * @param int Length of time to cache file locally before asking the server
  *            if it changed.
  * @return string File contents
  */
 function retrieveFile($url, $cacheLength, $cacheDir)
 {
     $cacheID = md5($url);
     $cache = new Cache_Lite(array("cacheDir" => $cacheDir, "lifeTime" => $cacheLength));
     if ($data = $cache->get($cacheID)) {
         return $data;
     } else {
         // we need to perform a request, so include HTTP_Request
         include_once 'HTTP/Request.php';
         // HTTP_Request has moronic redirect "handling", turn that off (Alexey Borzov)
         $req = new HTTP_Request($url, array('allowRedirects' => false));
         // if $cache->get($cacheID) found the file, but it was expired,
         // $cache->_file will exist
         if (isset($cache->_file) && file_exists($cache->_file)) {
             $req->addHeader('If-Modified-Since', gmdate("D, d M Y H:i:s", filemtime($cache->_file)) . " GMT");
         }
         $req->sendRequest();
         if (!($req->getResponseCode() == 304)) {
             // data is changed, so save it to cache
             $data = $req->getResponseBody();
             $cache->save($data, $cacheID);
             return $data;
         } else {
             // retrieve the data, since the first time we did this failed
             if ($data = $cache->get($cacheID, 'default', true)) {
                 return $data;
             }
         }
     }
     Services_ExchangeRates::raiseError("Unable to retrieve file {$url} (unknown reason)", SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
     return false;
 }
示例#2
0
 function _retrieveFile($url, $cacheLength, $cacheDir)
 {
     $cacheID = md5($url);
     $cache = new Cache_Lite(array("cacheDir" => $cacheDir, "lifeTime" => $cacheLength));
     if ($data = $cache->get($cacheID)) {
         return $data;
     } else {
         $fp = fopen($url, 'r');
         $data = stream_get_contents($fp);
         if (strlen($data) > 10) {
             // data is changed, so save it to cache
             $cache->save($data, $cacheID);
             return $data;
         } else {
             // retrieve the data, since the first time we did this failed
             if ($data = $cache->get($cacheID, 'default', true)) {
                 return $data;
             }
         }
     }
     Services_ExchangeRates::raiseError("Unable to retrieve file {$url} (unknown reason)", SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
     return false;
 }
示例#3
0
 /**
  * Downloads exchange rates in terms of the PLN from the National Bank of
  * Poland (NBP). This information is updated daily, and is cached by default for 1 hour.
  *
  * Returns a multi-dimensional array containing:
  * 'rates' => associative array of currency codes to exchange rates
  * 'source' => URL of feed
  * 'date' => date feed last updated, pulled from the feed (more reliable than file mod time)
  *
  * @link http://www.nbp.pl/Kursy/RatesA.html English HTML version
  * @link http://www.nbp.pl/Kursy/KursyA.html Polish HTML version (with link to XML)
  *
  * @param int Length of time to cache (in seconds)
  * @return array 
  */
 function retrieve($cacheLength, $cacheDir)
 {
     $return['rates'] = array('PLN' => 1.0);
     // retrieve XML address
     $htmlpage = $this->retrieveFile($this->feedHTMLUrl, $cacheLength, $cacheDir);
     // Example line is:
     // <div class="file"><a href="xml/a055z020319.xml">powysza tabela w formacie .xml</a></div>
     if (!preg_match('#href="(xml/a\\d+z\\d+\\.xml)"#', $htmlpage, $match)) {
         Services_ExchangeRates::raiseError("Retrieved url " . $this->feedHTMLUrl . " has no link to XML page", SERVICES_EXCHANGERATES_RETRIEVAL_FAILED);
         return false;
     }
     $this->feedXMLUrl = $this->feedDir . $match[1];
     $return['source'] = $this->_feedXMLUrl;
     // retrieve the feed from the server or cache
     $root = $this->retrieveXML($this->feedXMLUrl, $cacheLength, $cacheDir);
     // get down to array of exchange rates
     foreach ($root->children as $rateinfo) {
         if ($rateinfo->name == 'pozycja') {
             $rateinfo->children[4]->content = strtr($rateinfo->children[4]->content, ',', '.');
             $value = $rateinfo->children[2]->content / $rateinfo->children[4]->content;
             // currency rate
             $return['rates'][$rateinfo->children[3]->content] = $value;
         } elseif ($rateinfo->name == 'data_publikacji') {
             // set date published
             $return['date'] = $rateinfo->content;
         }
     }
     return $return;
 }