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.

Appreciate this content?

Sign up for our weekly newsletter, which delivers our latest posts every Monday morning.

We don’t spam! Read our privacy policy for more info.

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.