private function runRequest($path, $args = array()) { // print_r($args); // exit; $url = $this->baseUrl . $path . '?' . http_build_query($args); // echo $url ; // exit; $key = sha1('Eventbrite_API' . $url); if (!$this->enable_cache) { return json_decode($this->_runRequest($url)); } try { $data = Cache::get($key); return json_decode($data); } catch (\CacheNotFoundException $e) { /* Catching the CacheNotFoundException exception will catch both CacheNotFoundException and CacheExpiredException. Use this when catching the exception. */ $rawData = $this->_runRequest($url); Cache::set($key, $rawData, $this->expire * 60); return json_decode($rawData); } }
public function run($lembaga = 'DPR') { //First get all provinces, and then cache it $provinces = ''; try { $provinces = Cache::get('provinces'); } catch (\CacheNotFoundException $e) { $provinces = file_get_contents("http://api.pemiluapi.org/candidate/api/provinsi?apiKey=" . $this->apiKey); } //Get all partais available, to be used much later, this is just a preparatory step try { $parties = Cache::get('partais'); } catch (\CacheNotFoundException $e) { $parties = file_get_contents("http://api.pemiluapi.org/candidate/api/partai?apiKey=78cc1abf180f90c46500f924248e6173"); } $parties = json_decode($parties); $parties = $parties->data->results->partai; $provinces = json_decode($provinces); $provinces = $provinces->data->results->provinsi; //Get all dapils for every province foreach ($provinces as $province) { echo "Getting dapils from province " . $province->nama_lengkap . " within lembaga " . $lembaga . "\n"; $provinceId = $province->id; try { $dapils = Cache::get('dapils_' . $provinceId); } catch (\CacheNotFoundException $e) { $dapils = file_get_contents("http://api.pemiluapi.org/candidate/api/dapil?apiKey=78cc1abf180f90c46500f924248e6173&provinsi=" . $provinceId . "&lembaga=" . $lembaga); } $dapils = json_decode($dapils); $dapils = $dapils->data->results->dapil; foreach ($dapils as $dapil) { foreach ($parties as $party) { echo "\tGetting all calegs from partai " . $party->nama . ", dapil " . $dapil->nama_lengkap . " within lembaga " . $lembaga . "\n"; $dapilId = $dapil->id; $cacheKey = 'get_calegs_by_dapil_' . md5($dapilId . '|' . $party->id . '|' . $lembaga); Cache::delete($cacheKey); $calegs = file_get_contents('http://api.pemiluapi.org/candidate/api/caleg?apiKey=' . $this->apiKey . '&partai=' . $party->id . '&tahun=2014&lembaga=' . $lembaga . '&dapil=' . $dapilId); Cache::set($cacheKey, $calegs); } } } }
/** * Japanese holidays * @author Y.Hasegawa <*****@*****.**> * @return array */ public static function get_holidays() { $identifier = 'holidays'; try { $cache = \Fuel\Core\Cache::get($identifier); if (is_array($cache)) { return $cache; } } catch (\Fuel\Core\CacheNotFoundException $ex) { } $url = 'https://calendar.google.com/calendar/ical/japanese__ja%40holiday.calendar.google.com/public/full.ics'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $ics = curl_exec($ch); curl_close($ch); $holidays = array(); $day = null; foreach (explode("\n", $ics) as $line) { $line = trim($line); if (substr($line, 0, 8) == 'DTSTART;') { $array = explode(':', $line); $day = $array[1]; } if (substr($line, 0, 8) == 'SUMMARY:') { $array = explode(':', $line); $holidays[$day] = $array[1]; } } ksort($holidays); \Fuel\Core\Cache::set($identifier, $holidays, 86400); return $holidays; }