Permalink
Browse files

OCD tab2space

  • Loading branch information...
1 parent a2f18a7 commit e027bc12ea9382a9952d95f190c7f9a4afc831d6 unknown committed Apr 10, 2012
Showing with 74 additions and 74 deletions.
  1. +66 −66 crpapi.php
  2. +8 −8 example.php
View
@@ -10,82 +10,82 @@
class crp_api {
- function __construct($method=NULL,$params=NULL) {
+ function __construct($method=NULL,$params=NULL) {
- $this->api_key = "";
- $this->base_url = "http://api.opensecrets.org/";
- $this->output = "json";
-
- //Allow output type to be overridden on object instantiation
- $this->output = isset($params['output']) ? $params['output']: $this->output;
- $this->method = $method;
- self::load_params($params);
-
- $this->file_hash = md5($method . "," . implode(",",$params));
- $this->cache_hash = "dataCache/" . $this->file_hash;
- $this->cache_time = 86400; #one day
-
- }
+ $this->api_key = "";
+ $this->base_url = "http://api.opensecrets.org/";
+ $this->output = "json";
+
+ //Allow output type to be overridden on object instantiation
+ $this->output = isset($params['output']) ? $params['output']: $this->output;
+ $this->method = $method;
+ self::load_params($params);
+
+ $this->file_hash = md5($method . "," . implode(",",$params));
+ $this->cache_hash = "dataCache/" . $this->file_hash;
+ $this->cache_time = 86400; #one day
+
+ }
- private function load_params($params) {
- $this->url = $this->base_url . "?method=" . $this->method .
- "&apikey=" . $this->api_key;
+ private function load_params($params) {
+ $this->url = $this->base_url . "?method=" . $this->method .
+ "&apikey=" . $this->api_key;
- foreach ($params as $key=>$val) {
- $this->url .= "&" . $key . "=" . $val;
- $this->$key = $val;
- }
+ foreach ($params as $key=>$val) {
+ $this->url .= "&" . $key . "=" . $val;
+ $this->$key = $val;
+ }
- return;
- }
-
- public function get_data($use_cache=true) {
-
- if ($use_cache and file_exists($this->cache_hash) and (time() - filectime($this->cache_hash) < $this->cache_time)) {
-
- $this->cache_hit = true;
- $file = fopen($this->cache_hash,"r");
- $this->data = stream_get_contents($file);
- $this->data = gzuncompress($this->data);
- $this->data = unserialize($this->data);
- fclose($file);
+ return;
+ }
+
+ public function get_data($use_cache=true) {
+
+ if ($use_cache and file_exists($this->cache_hash) and (time() - filectime($this->cache_hash) < $this->cache_time)) {
+
+ $this->cache_hit = true;
+ $file = fopen($this->cache_hash,"r");
+ $this->data = stream_get_contents($file);
+ $this->data = gzuncompress($this->data);
+ $this->data = unserialize($this->data);
+ fclose($file);
$this->response_headers = "No http request sent, using cache";
- } else {
- $this->cache_hit = false;
- $this->data = file_get_contents($this->url);
+ } else {
+ $this->cache_hit = false;
+ $this->data = file_get_contents($this->url);
$this->response_headers = $http_response_header;
- switch ($this->output) {
- case "json":
- $this->data = json_decode($this->data,true);
- break;
- case "xml":
- $this->data = simplexml_load_string($this->data);
- break;
- default:
- die("Unknown output type. Use 'json' or 'xml'");
- }
+ switch ($this->output) {
+ case "json":
+ $this->data = json_decode($this->data,true);
+ break;
+ case "xml":
+ $this->data = simplexml_load_string($this->data);
+ break;
+ default:
+ die("Unknown output type. Use 'json' or 'xml'");
+ }
- if ($use_cache) {
- $file = fopen($this->cache_hash,"w");
- $store = serialize($this->data);
- $store = gzcompress($store);
- fwrite($file,$store);
- fclose($file);
- }
- }
-
- return $this->data;
- }
-
- function get_cache_status() {
- return $this->cache_hit;
- }
+ if ($use_cache) {
+ $file = fopen($this->cache_hash,"w");
+ $store = serialize($this->data);
+ $store = gzcompress($store);
+ fwrite($file,$store);
+ fclose($file);
+ }
+ }
+
+ return $this->data;
+ }
+
+ function get_cache_status() {
+ return $this->cache_hit;
+ }
- function get_response_headers() {
- return $this->response_headers;
- }
+ function get_response_headers() {
+ return $this->response_headers;
+ }
}
?>
View
@@ -58,9 +58,9 @@
echo "<h2>Request Cache Status</h2>";
if ($crp->get_cache_status()) {
- echo "Cache Hit";
+ echo "Cache Hit";
} else {
- echo "Cache Miss";
+ echo "Cache Miss";
}
echo "<hr />";
@@ -90,17 +90,17 @@
echo "<h3>Meta data</h3>";
foreach ($data['response']['industries']['@attributes'] as $key=>$val) {
- echo $key . " => " . $val . "<br />";
+ echo $key . " => " . $val . "<br />";
}
echo "<h3>Actual data</h3>";
echo "<table><tr><th>Industry</th><th>Indivs</th><th>PACs</th><th>Total</th></tr>";
foreach ($data['response']['industries']['industry'] as $ind) {
- foreach ($ind as $row) {
- echo "<tr><td>" . $row['industry_name'] . "</td><td>$" .
- $row['indivs'] . "</td><td>$" . $row['pacs'] . "</td><td>$" .
- $row['total'] . "</td></tr>";
- }
+ foreach ($ind as $row) {
+ echo "<tr><td>" . $row['industry_name'] . "</td><td>$" .
+ $row['indivs'] . "</td><td>$" . $row['pacs'] . "</td><td>$" .
+ $row['total'] . "</td></tr>";
+ }
}
echo "</table>";

0 comments on commit e027bc1

Please sign in to comment.