This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

eRep API Compressed Feed Basic Class

By: a guest on Oct 25th, 2010  |  syntax: PHP  |  size: 1.32 KB  |  views: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3.  * eRep API Compressed Feed Basic Class
  4.  * @author Kshitij Parajuli
  5.  */
  6.  
  7. Class FeedReader{
  8.  
  9.         public $country_feed_gz;
  10.         public $country_feed;
  11.        
  12.         /*
  13.          * Get the country ID we are messing with and download the file
  14.          * @param Int $country_id ID of the country
  15.          * @return Location of the downloaded GZipped file
  16.          */
  17.         public function __construct($country_id){
  18.                 $country_id = (int)$country_id;
  19.                 $file = "http://api.erepublik.com/v2/feeds/countries/$country_id/citizens.xml.gz";
  20.                 $destination = "feeds/citizens_".$country_id.".xml.gz";
  21.                 $content = file_get_contents($file);
  22.                 file_put_contents($destination, $content);
  23.                 $this->country_feed_gz = $destination;
  24.                 return $destination;
  25.         }
  26.        
  27.         /*
  28.          * Extract the GZ file downloaded from the eRep server
  29.          * @param Int $buffer_size Buffer Size in Bytes (Optional)
  30.          * @return Location of the extracted xml file
  31.          */
  32.         public function extract_gz($buffer_size=262400){
  33.                 $file_name = $this->country_feed_gz;
  34.                
  35.                 $out_file_name = str_replace('.gz', '', $file_name);
  36.                 $file = gzopen($file_name, 'rb');
  37.                 $out_file = fopen($out_file_name, 'wb');
  38.                 while(!gzeof($file)) {
  39.                     fwrite($out_file, gzread($file, $buffer_size));
  40.                 }
  41.                 fclose($out_file);
  42.                 gzclose($file);
  43.                
  44.                 $this->country_feed = $out_file_name;
  45.                
  46.                 return $out_file_name;
  47.         }
  48.        
  49.        
  50. }
  51.  
  52. ?>
clone this paste RAW Paste Data