Google has had a URL shortening domain for quite a while now. I took a few minutes to review their API and created a very basic function where you pass the URL and the key (http://code.google.com/apis/urlshortener/ ) and it will do the magic for you.
PHP Function
function get_short_url($key, $url)
{
$postData = array('longUrl' => $url, 'key' => $key);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
// Change the response json string to object
$json = json_decode($response, true);
curl_close($curlObj);
return isset($json['id']) ? $json['id'] : false;
}
Parameters:
$key - You API key
$url - Long URL which needs to be shortened
Return Parameter:
FALSE - if url is not shortened
short url - if url is shortened
Note: This code is a tested code and used to run at QueryHome before we moved to bit.ly