Create tinyurls in PHP using tinyurl.com API
For all those unfamiliar with tinyurl, it is a service thats lets you convert long urls into short ones. It is very popular and gets about 1.5 billion hits per month. Use of tinyurl can be seen hugely in applications like twitter. As twitter limits users to enter 140 character messages, long urls are a pain to post. Tinyurl reduces them to 25-30 characters. Then they can be posted easily along with other message.
Although it is not mentioned anywhere on TinyURL site, it has an API using which you can write a utility in PHP to create your own tiny urls.
The API can be found at
http://tinyurl.com/api-create.php?url=
It expects a parameter url which is the URL you wish to convert into tiny url. Return value is the generated tiny url in case of success, error otherwise.
Here is the php code:
$url = "http://vijayjoshi.org";
$tiny = file_get_contents("http://tinyurl.com/api-create.php?url=$url");
echo 'Original url : '.$url;
echo '
';
echo '
';
echo 'TinyURL (Click to test) : '.$tiny.'';
In the above case it will output
http://tinyurl.com/cn3c5d
Pretty straightforward. $url contains the url you wish to convert. Then we pass it along with the API url and finally use php function file_get_contents to get the output.
Go ahead and give it a try.
Give this article a Thumbs-up on Stumbleupon if you liked it.
Related Posts
2 Comments to “Create tinyurls in PHP using tinyurl.com API”
Add Comments (+)-
Thanks for sharing the tip.

nice idea! thanks