Recently I put together a PHP class so I could do my own short URLS partly because I saw shaun inman doing in on twitter and partly because it seemed like a fun little side project. So away I went and a few hours later I had my own short URL and quick2.eu was born. (this is open for public use at the moment but may not be in the future)
I use Tweetie as my main client for communicating with people on Twitter, if you have yet to use this I would suggest giving it a go, their is no cost unless you want to have the adverts removed. Anyway I thought it would be cool if Tweetie supported custom URL shorteners, alas it doesn’t.
First step was to ask atebits the developers behind Tweetie but hearing from atebits after making a feature request gave me some hope as there reply was that it was a heavy suggestion, which to me says that this is not the first time by far that this has been requested.
The only thing was this isn’t quick enough for me (it some times pays to be inpatient) knowing how the bit.ly API works i set to work with the intent to intersect the transmission and use my own servers.
here are the steps I made to make this possible.
first I updated my /etc/hosts file so that it would point api.bit.ly to the IP address of my web server.
Open up terminal and type;
sudo pico /etc/hosts
You will then be asked to enter your admin password so that you may edit this file. (pico is a simple text editor for mac terminal)
123.123.123.123 api.bit.ly
I Then needed to setup Apache so that it would accept api.bit.ly as if it was part of its own domain. To do this I needed to edit the apache config for the virtual host so it included this ‘ServerAlias api.bit.ly’. A simpler way to do if you web hosting solution supports this is to just setup a domain alias of api.bit.ly for the same domain as your short URL.
With all this technical stuff out the way the next step is to impelment the same process that the bit.ly’s API does.
I Went to my domain created a folder called ‘shorten’ then created a file called index.php which look a bit like the following.
// include my Class and run with the URL that requires shortening. require_once "../class.quick2.php"; $quick2 = new quick2(); $newlink = $quick2->addLink($_GET['longUrl']); // output a simple JSON string that contains the new short URL echo '{"shortUrl": "'.$newlink.'" }';
This is would be a bit different for most as this would only work with my own Class that I have not yet made public but the main idea is to take the variable $_GET['longUrl'] and convert that into a new short URL.
Job Done
Any news on the class ?:)
Having some problems making sure the URL is valid, also putting in some JSON stuff to match the bit.ly API, still planing to make it public though.