00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 class REST
00012 {
00013 private $base;
00014 private $url;
00015
00016 public static $cacheResults = true;
00017
00018 public static $cacheLife = 86400;
00019
00020 public function __construct($url)
00021 {
00022
00023 $this->base = $url;
00024 }
00025
00026 public function addParam($key, $val)
00027 {
00028 $this->setUrlParam($key, $val);
00029 }
00030
00031 public function addParams($vars = array())
00032 {
00033
00034 if (count($vars))
00035 foreach ($vars AS $key => $val)
00036 $this->addParam($key, $val);
00037 }
00038
00039 public function getUrl()
00040 {
00041 return $this->url;
00042 }
00043
00044 public function getLink($text = 'View Response')
00045 {
00046 return "<a href=\"" . $this->getUrl() . "\">$text</a>";
00047 }
00048
00049 public function request($vars = array())
00050 {
00051
00052 $this->url = $this->base;
00053
00054
00055 $this->addParams($vars);
00056
00057
00058 $data = $this->getData();
00059
00060 return $data;
00061 }
00062
00063 private function getData()
00064 {
00065
00066 $data = '';
00067
00068
00069 $data = $this->getCacheData();
00070
00071
00072 if (!$data)
00073 {
00074
00075 $data = $this->getRemoteData();
00076
00077
00078 $this->saveCacheData($data);
00079 }
00080
00081
00082 return $data;
00083 }
00084
00085 protected function getCacheKey()
00086 {
00087 return "Rest:" . get_class($this) . ":" . sha1($this->url);
00088 }
00089
00090 protected function getCacheData()
00091 {
00092 if (self::$cacheResults)
00093 {
00094
00095 $key = $this->getCacheKey();
00096
00097
00098 return CacheBot::get($key, self::$cacheLife);
00099 }
00100 }
00101
00102 protected function saveCacheData($data)
00103 {
00104 if (self::$cacheResults)
00105 {
00106
00107 $key = $this->getCacheKey();
00108
00109
00110 return CacheBot::set($key, $data, self::$cacheLife);
00111 }
00112 }
00113
00114 private function getRemoteData()
00115 {
00116 return $this->curlLoadUrl();
00117 }
00118
00119 private function curlLoadUrl()
00120 {
00121 $ch = curl_init();
00122 $timeout = 5;
00123 curl_setopt ($ch, CURLOPT_URL, $this->url);
00124 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
00125 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
00126 $file_contents = curl_exec($ch);
00127 curl_close($ch);
00128
00129 return $file_contents;
00130 }
00131
00132 private function setUrlParam($name, $value)
00133 {
00134
00135 $url = $this->url;
00136
00137
00138 if ($pos = strpos($url, '?'))
00139 {
00140 $vars = substr($url, $pos+1);
00141 $url = substr($url, 0, $pos);
00142 }
00143
00144
00145 $getAr = array();
00146 $paramAr = explode('&', $vars);
00147 foreach ($paramAr AS $param)
00148 {
00149 if ($param)
00150 {
00151 $vars = explode('=', $param);
00152 $getAr[$vars[0]] = $vars[1];
00153 }
00154 }
00155
00156
00157 $getAr[$name] = urlencode($value);
00158
00159
00160 $count = 0;
00161 $url .= "?";
00162 foreach ($getAr AS $key => $val)
00163 {
00164 if ($count)
00165 $url .= '&';
00166
00167 $url .= "$key=$val";
00168
00169 $count++;
00170 }
00171
00172
00173 $this->url = $url;
00174 }
00175 }
00176 ?>