1. <?php
  2. /**
  3. * class php-crpapi
  4. * Simple PHP client library for working with the Center for Responsive Politics' API.
  5. * Information on CRP's API can be found at http://www.opensecrets.org/action/api_doc.php
  6. * Information on using this class, including examples at http://github.com/bpilkerton/php-crpapi
  7. * @author Ben Pilkerton <bpilkerton@gmail.com>
  8. * @version 0.2
  9. */
  10.  
  11. class Crp {
  12.  
  13.     function __construct($params) {
  14.    
  15.         $this->apikey = "a04a90aa18e9e482b3ff2318d313db53";
  16.         $this->baseurl = "http://api.opensecrets.org/";
  17.         $this->output = "json";
  18.        
  19.         //Allow output type to be overridden on object instantiation
  20.         $this->output = isset($params['output']) ? $params['output']: $this->output;
  21.         $this->method = $params['method'];
  22.         self::loadParams($params);
  23.        
  24.         /*$this->fileHash = md5($method . "," . implode(",",$params));
  25.         $this->cacheHash = "dataCache/" . $this->fileHash;
  26.         $this->cacheTime = 86400; #one day*/
  27.    
  28.     }
  29.  
  30.     private function loadParams($params) {
  31.         $this->url = $this->baseurl . "?method=" . $this->method .
  32.         "&apikey=" . $this->apikey;
  33.    
  34.         foreach ($params as $key=>$val) {
  35.             $this->url .= "&" . $key . "=" . $val;
  36.             $this->$key = $val;
  37.         }
  38.    
  39.         return;
  40.     }
  41.  
  42.     public function getData($useCache=false) {
  43.    
  44.         if ($useCache and file_exists($this->cacheHash) and (time() - filectime($this->cacheHash) < $this->cacheTime)) {
  45.        
  46.             $this->cacheHit = true;
  47.             $file = fopen($this->cacheHash,"r");
  48.             $this->data = stream_get_contents($file);
  49.             $this->data = gzuncompress($this->data);
  50.             $this->data = unserialize($this->data);
  51.             fclose($file);
  52.        
  53.         } else {
  54.             $this->cacheHit = false;
  55.             $this->data = file_get_contents($this->url);
  56.            
  57.             switch ($this->output) {
  58.                 case "json":
  59.                 $this->data = json_decode($this->data,true);
  60.                 break;
  61.                 case "xml":
  62.                 $this->data = simplexml_load_string($this->data);
  63.                 break;
  64.                 default:
  65.                 die("Unknown output type. Use 'json' or 'xml'");
  66.             }
  67.        
  68.             if ($useCache) {
  69.                 $file = fopen($this->cacheHash,"w");
  70.                 $store = serialize($this->data);
  71.                 $store = gzcompress($store);
  72.                 fwrite($file,$store);
  73.                 fclose($file);
  74.             }
  75.         }
  76.        
  77.         return $this->data;
  78.         }
  79.        
  80.     function getCacheStatus() {
  81.         return $this->cacheHit;
  82.     }
  83.  
  84. }