timebomb.it API and PHP Wrapper Class
If you haven’t read anything about our newest web and mobile app timebomb.it, make sure to stop by Send Time Limited Secure Logins with timebomb.it. Lets take a look at the simple API we made so developers can use timebomb.it for anything they please. Remember, you must have an API key to use it- just contact us and we’ll shoot one right back to you.
Using the API with JSON
We require four pieces of information for the API to process successfully: the API key, a username, a password, and an expiration value. If you have tried out timebomb.it, this should make perfect sense. The API is accessed through the following URL structure:
https://timebomb.it/api/json/APIKEY/USERNAME/PASSWORD/EXPIRATION
The only somewhat non-standard entry is the EXPIRATION value. This will be set to either a 1, 2, or 3, corresponding to 1-hour, 1-day, or 1-week expiration time respectively. The output will be a JSON encoded string with the following keys:
username : provided username
password : provided password
url : timebomb generated URL
expiration : seconds until link expires
created : unix timestamp
Example Input:
https://timebomb.it/api/json/Y8dqEisNoAChS6AFmwyQ1LoJ/elvis/bluesuedeshoes/1
Example output:
{“username”:”elvis”,”password”:”bluesuedeshoes”,”url”:
“https:\/\/timebomb.it\/4dewszeaoa”,”expiration”:3600,”created”:1282529729}
We suggest reading the HTTP header status in order to gain details on the success or failure of the request. The following statuses are returned based on the request:
Status = 202 : Request Successful
Status = 404 : Request Denied, Check Data Structure
Status = 500 : Request Denied, Possible timebomb.it Error
PHP Wrapper Class using cURL
We have created a basic PHP wrapper that can be used as a standalone class, making it pretty simple to integrate timebomb.it into your application. Of course it’s not perfect, that will be subject to your application, but it will get you off the ground quickly. We are using the PHP library cURL, which you probably have if PHP is installed on your server, otherwise look at How to Install cURL for PHP. Below is the standard usage for the timebomb.class.php wrapper.
<?php
require_once('timebomb.class.php');
$tb = new Timebomb();
$data = array(
"key"=>"Y8dqEisNoAChS6AFmwyQ1LoJ",
"username"=>"elvis",
"password"=>"bluesuedeshoes",
"expiration"=>"1"
);
$timebomb = $tb->create_link($data);
if($timebomb['success']) {
echo $timebomb['url'];
} else {
echo $timebomb['message'];
}
/*$timebomb_data array elements
$timebomb['success'] : TRUE or FALSE
$timebomb['message'] : status message
$timebomb['username'] : provided username
$timebomb['password'] : provided password
$timebomb['url'] : TimeBomb generated URL
$timebomb['expiration'] : seconds until link expires
$timebomb['created'] : unix timestamp
*/
//Use for auto-generated passwords
//$password = $tb->create_password();
We have added a simple password generator to the timebomb.class.php wrapper as well, just in case you want to produce them on the fly. The “timebomb” array returned from the class is described above in the PHP comments, and provides all the important information about the created link, or if there was an error. Below is the source for the timebomb.class.php wrapper, and we have provided both of these files in a ZIP package for convenience.
<?php
class Timebomb {
function create_link($timebomb_info) {
$url = implode('/',$timebomb_info);
$ch = curl_init('https://timebomb.it/api/json/'.$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_data = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
if($curl_data != FALSE) {
$timebomb_data['status'] = $curl_info['http_code'];
switch ($timebomb_data['status']) {
case 202:
$json_data = substr($curl_data, $curl_info['header_size']);
$timebomb_data = json_decode($json_data,TRUE);
$timebomb_data['success'] = TRUE;
$timebomb_data['message'] = 'Congrats, your TimeBomb link was created.';
return $timebomb_data;
break;
case 404:
$timebomb_data['success'] = FALSE;
$timebomb_data['message'] = 'Please check your inputs and make sure you have a valid TimeBomb API key.';
return $timebomb_data;
break;
case 500:
$timebomb_data['success'] = FALSE;
$timebomb_data['message'] = 'TimeBomb may have blown itself up, please try again.';
return $timebomb_data;
break;
default:
$timebomb_data['message'] = 'Houston, we have a problem with something, not sure who to pin it on right now.';
$timebomb_data['success'] = FALSE;
return $timebomb_data;
}
}
$timebomb_data['message'] = 'The cURL process failed.';
$timebomb_data['success'] = FALSE;
return $timebomb_data;
}
function create_password($length=12) {
$chars = array_merge(range('a', 'z'),range('A', 'Z'),range(0, 9));
$password ='';
for($i=0;$i < $length;$i++) {
$password .= $chars[mt_rand(0,count($chars)-1)];
}
return $password;
}
}
Download the timebomb.class.php ZIP Package
Final Remarks
If you would like to contribute, collaborate, or provide feedback to our timebomb.it project, please be sure to visit the Prime Studios Homepage, or comment on this post. Happy coding everybody.
Comments