Content Marketing
PHP: Using Is.gd API to Shorten URL

Just read an article on the benefits and pitfalls of shortening URLs over at SEOmoz. I utilize the Is.gd API to do this:
function doCurlRequest($url, $variable, $value) {
$api = $url."?".$variable."=".$value;
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $api);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($session);
curl_close($session);
return $data;
}
function doShortenURL($longurl) {
$url = "http://is.gd/api.php";
$variable = "longurl";
$shorturl = doCurlRequest($url, $variable, $longurl);
return $shorturl;
}
Thought you folks might appreciate an actual code sample. Be sure to replace the funkified quotes that posting this to the blog adds. To use, just add the above functions to your PHP page and then execute like this:
doShortenURL('http://thisis.my/long/url/with?lots=of&data=');
I would probably add a try/catch block just to be sure. This code works with PHP 5+ with the cURL library enabled. If your host doesn’t enable cURL, find a new host.