Skip to content

Instantly share code, notes, and snippets.

@donutdan4114
Created May 24, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donutdan4114/46127033407e21760fe6 to your computer and use it in GitHub Desktop.
Save donutdan4114/46127033407e21760fe6 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* @file
* Small script for interacting with the tinycache.io API.
*
* https://tinycache.io/docs
*
* Usage examples:
* > tinycache
* Gets available cache_keys and usage data.
*
* > tinycache get mykey
* Does a GET request for mykey.
*
* > tinycache get MyEncryptedData decrypt=this-is-my-decryption-key
* Does a GET request with the decrypt query parameter.
*
* > tinycache post new_key '{"cache_value":"new value here"}'
* Adds a new value to the tinycache. (You can also say "create").
*
* > tinycache delete new_key
* Deletes the key from the server.
*
* > tinycache put existing_key '{"expire":3600}'
* Updates existing_key to expire in one hour. (You can also say "update").
*
* @author Daniel Pepin <me@danieljpepin.com>
*/
/**
* Set your own API key here.
*
* Or export your API key.
* > export TINYCACHE_API_KEY=your_api_key_here
*/
define('API_KEY', $_SERVER['TINYCACHE_API_KEY']);
// Don't need the first arg.
array_shift($argv);
// Create the curl handle.
$ch = curl_init();
// Set the default url.
$service_url = 'https://tinycache.io/api/v1/' . API_KEY;
// Handle different methods.
if (isset($argv[0])) {
$argv[0] = ($argv[0] == 'update') ? 'put' : $argv[0];
$argv[0] = ($argv[0] == 'create') ? 'post' : $argv[0];
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($argv[0]));
}
// Handle cache_key.
if (isset($argv[1])) {
$service_url .= '/' . $argv[1];
}
// Handle passing data.
if (isset($argv[2])) {
if ($argv[0] == 'get') {
$service_url .= '?' . $argv[2];
}
else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $argv[2]);;
}
}
// Make the request.
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_response = curl_exec($ch);
curl_close($ch);
// Show you what we got.
print $curl_response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment