With the explosion of Twitter and such services, the need to have long URLs shortened has drastically increased. Naturally, many services to shorten URLs have sprung up as well.

Most of these services (that are worth using), have some sort of API that will allow developers to send a long URL to them, have it shortened and then returned. This type of service allows other developers to write applications that have the ability to shorten URLs right inside of them.

Recently, I had to write a PHP class to work with URL shorteners. So, I made it support multiple shortening services and have decided to share it with you.

The class is called URLShortener.class.php and it supports the following shortening services:

Here are some examples of how to use the class in your own applications:

<code class=’php’><?php
     /* Basic: */
     $s = new URLShortener(‘tr.im’);
     echo $s->shorten("http://brandontreb.com");

/* Using API Key and Login */ $s = new URLShortener(‘j.mp’); $s->login = ‘brandontreb’; $s->APIKey = ‘R_2a413ebd15254a72b500ec2ce83f982d’; echo $s->shorten(“http://brandontreb.com”); /* Custom URL */ // Just make sure you put %s and it will be // replaced with the long URL $s = new URLShortener(‘custom’, ‘http://b1t.me/api/shorten.xml/%s’); echo $s->shorten("http://brandontreb.com",false);


You may download my URLShortener PHP class here.

Feel free to leave any questions or comments in the comments section.