3, 'posts/recent' => 60, 'posts/all' => 300 ]); SDV($PinboardAPICacheFolder, 'pub/cache/pinboard/'); SDV($PinboardAPIResponseCacheDuration, 0); $PinboardAPIResponseCacheDuration = ($PinboardAPIResponseCacheDuration > 0) ? max($PinboardAPIResponseCacheDuration, max($PinboardAPIMethodCooldowns)) : 0; /*********/ /* SETUP */ /*********/ if (!file_exists($PinboardAPICacheFolder)) mkdir($PinboardAPICacheFolder); $response_cache_file = $PinboardAPICacheFolder."response_cache.json"; $response_cache = PinboardAPIGetResponseCache(); $request_log_file = $PinboardAPICacheFolder."request_log.json"; $request_log = PinboardAPIGetRequestLog(); /*************/ /* FUNCTIONS */ /*************/ function PinboardAPIRequest($method, $params) { global $PinboardAPIToken, $PinboardAPIEndpoint, $PinboardAPIAllowedMethods, $PinboardAPIResponseCacheDuration, $PinboardAPIMethodCooldowns; global $request_log, $response_cache; ## Check if specified method is allowed. If not, return an error. if (!(in_array($method, $PinboardAPIAllowedMethods) || (in_array('notes/ID', $PinboardAPIAllowedMethods) && preg_match("^notes\/", $method)))) return [ 'error-text' => "The method “$method” is not permitted.", 'error-html' => "
The method “$method
” is not permitted.
Too many requests. Wait a bit, then try again.
\n" ]; ## Send the request. $curl = curl_init($request_URL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $curl_response = curl_exec($curl); ## Handle the response. $info = curl_getinfo($curl); $response = json_decode($curl_response, true); $response['http_code'] = $info['http_code']; $response['request_time'] = $cur_time; curl_close($curl); ## Cache the response. PinboardAPIUpdateResponseCache($request_URL_hash, $response); ## Update the last-request info. $request_log['last-global'] = $cur_time; if ($method == 'posts/recent') $request_log['last-posts/recent'] = $cur_time; else if ($method == 'posts/all') $request_log['last-posts/all'] = $cur_time; $request_log['last_request_hash'] = $request_URL_hash; PinboardAPISaveRequestLog($request_log); return $response; } /***************/ /* REQUEST LOG */ /***************/ function PinboardAPIResetRequestLog() { global $request_log_file; file_put_contents($request_log_file, json_encode([ 'last-global' => 0, 'last-posts/recent' => 0, 'last-posts/all' => 0 ])); } function PinboardAPIGetRequestLog() { global $request_log_file; if (!file_exists($request_log_file)) PinboardAPIResetRequestLog(); $request_log = json_decode(file_get_contents($request_log_file), true); return $request_log; } function PinboardAPISaveRequestLog($request_log) { global $request_log_file; file_put_contents($request_log_file, json_encode($request_log)); } /******************/ /* RESPONSE CACHE */ /******************/ function PinboardAPIClearResponseCache() { global $response_cache_file; file_put_contents($response_cache_file, json_encode([ ])); } function PinboardAPIGetResponseCache() { global $response_cache_file; if (!file_exists($response_cache_file)) { PinboardAPIClearResponseCache(); return [ ]; } else { return json_decode(file_get_contents($response_cache_file), true); } } function PinboardAPIUpdateResponseCache($request_URL, $response) { global $response_cache_file, $PinboardAPIResponseCacheDuration; $response_cache = ($PinboardAPIResponseCacheDuration > 0) ? PinboardAPIGetResponseCache() : [ ]; $response_cache[$request_URL] = $response; file_put_contents($response_cache_file, json_encode($response_cache)); }