Tinyby.com – The API to generate and resolve short links

This post was written by Brandon on July 20, 2009
Posted Under: General,PHP

My project Tinyby.com has been in slow development. Recently an API system has been added to allow scripts to make use of shortening URLS, proxified or not, and with or without a custom short name, and even resolving them.

The API resolves around the php function file_get_contents, which was previously used here: Website parsing for specific data.

Although all the instructions for making use of the API can be found on the developers page, I will go over it in more detail here.

These functions are currently for PHP only, but they will be moved to other programming languages soon. Or you can learn how to Download the contents of a website in Java and try to build an API in Java for yourself!

More in the full article...



The first function allows creation of the shortened URL. The function only has one required parameter and only returns one thing, the shortened URL or the error messages.

To start, here is the function:

 
function createRedirect($url,$short='',$proxified='')
	{
		$url = str_replace('&','(*)',$url);
		$shorturl= file_get_contents('http://tinyby.com/api.php?u='.$url.'&s='.$short.'&p='.$proxified);
		return $shorturl;
	}
 

api.php on my server recognizes not to do anything if it recieves nothing, or a blank string for $short or $proxified.

To tell the URL to become proxified, you can pass that parameter a '1', 'yes' or a 'true'.

One of the first lines within the function is this:

 
$url = str_replace('&','(*)',$url);
 

What this does is convert all the & symbols within the link we are passing to a special symbol we know will never appear in the url. Then, on the server side, the symbol is replaced back with & symbols. Without this, the shortened URLS will be truncated at the &.

Through doing file_get_contents with the appropriate parameters, the server will simply print out the URL. There will be no formatting on the page, just plain text, allowing it to return just the shortened url.

Usage of this function is as easy as copying it within your php code, and doing something similar to the following:

 
$mynewlink = createRedirect('http://BlindingProxy.com','blinding');
echo $mynewlink;
 

As you can see, that will create a redirect to BlindingProxy.com with the short url blinding. If the short url is taken, a random one will be generated and returned.
Below are some of the errors that can be returned from the server

Errors:

A URL was not entered. Please try again --> Nothing was passed through the function to the server

Invalid Proxy Short -> Only A-Z, 0-9, dashes and underscores are allowed

Proxy short can be a max of 30 characters --> Self explanatory

URL can not exceed 2000 characters! --> Also self explanitory

The last function simply returns the full url from the shortened one.

 
	function resolveRedirect($url)
	{
		$resolvedurl= file_get_contents('http://tinyby.com/apiget.php?u='.$url);
		return $resolvedurl;
	}
 

All it needs is the one parameter and it will return the full url.

The api will be extended later on to be able to see if a url is proxified or not, as well as seeing traffic stats for links and whatever other new features are released.

Tinyby.com - Proxified url redirection

Questions? Comments? Ask below!

Add a Comment

required, use real name
required, will not be published
optional, your blog address