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.

Douglas Karr

Douglas Karr is a fractional Chief Marketing Officer specializing in SaaS and AI companies, where he helps scale marketing operations, drive demand generation, and implement AI-powered strategies. He is the founder and publisher of Martech Zone, a leading publication in marketing technology, and a trusted advisor to startups and enterprises… More »
Back to top button
Close

Adblock Detected

We rely on ads and sponsorships to keep Martech Zone free. Please consider disabling your ad blocker—or support us with an affordable, ad-free annual membership ($10 US):

Sign Up For An Annual Membership