Shortening URL Using bit.ly Web Service API

Having a shortened url would be handy sometimes, especially when you need to include that in an E-Mail message to avoid splitting the long url into multiple lines or in a Twitter status message that has the 140 character limit.

Though shortening of url can be done using the many services like tinyurl.com or bit.ly, it has to be done using their API, if the message is constructed and posted from some application. My favorite site to do that is http://bit.ly and they offer a simple API. The following simple class explains how a shortened url is obtained programmatically.

ShortUrl.php

<?php
   include_once 'CurlWrap.php';

   define('BIT_LY_USERNAME','YOUR-BIT-LY-USERNAME');
   define('BIT_LY_API_KEY','YOUR-BIT-LY-APIKEY');
   define('BIT_LY_SHORTEN_API','http://api.bit.ly/shorten?version=2.0.1');

   class ShortUrl
   {
      private $curl;

      function __construct()
      {
         $this->curl=new CurlWrap();
         $this->curl->setOption(CURLOPT_POST,false);
      }

      function shortenUrl($url)
      {
         $api_call=BIT_LY_SHORTEN_API. "&longUrl=". urlencode($url). "&login=". BIT_LY_USERNAME. "&apiKey=". BIT_LY_API_KEY. "&format=xml";

     $this->curl->exec($api_call);
         $http_code=$this->curl->getHttpCode();
     if ($http_code==200||$http_code==201) {
        $response=$this->curl->getExecResponse();
        $xml=new SimpleXmlElement($response);
        return (string)$xml->results->nodeKeyVal->shortUrl;
     }
         else return false;
      }
   }
?>

You need to open an account and obtain an apiKey to use the API. The utility class CurlWrap is described <a href=”https://smoothops.wordpress.com/2010/07/16/using-curl-to-make-rest-api-calls/”>here</a&gt;.

From a PHP script, this class can be used as in the following example:

<?php
include_once 'ShortUrl.php';

$shorturl=new ShortUrl();
print $shorturl->shortenUrl('http://www.nytimes.com/2010/07/23/opinion/23krugman.html')."\n";

?>

Execution of this script got me this: “http://nyti.ms/d9U2nC&#8221;. Looks like New York Times has a special arrangement with bit.ly 🙂

<pre style=”font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%”><code>&lt;?php
include_once ‘CurlWrap.php’;

define(‘BIT_LY_USERNAME’,’YOUR-BIT-LY-USERNAME’);
define(‘BIT_LY_API_KEY’,’YOUR-BIT-LY-APIKEY’);
define(‘BIT_LY_SHORTEN_API’,’http://api.bit.ly/shorten?version=2.0.1&#8242;);

class ShortUrl
{
private $curl;

function __construct()
{
$this-&gt;curl=new CurlWrap();
$this-&gt;curl-&gt;setOption(CURLOPT_POST,false);
}

function shortenUrl($url)
{
$api_call=BIT_LY_SHORTEN_API. &quot;&amp;longUrl=&quot;. urlencode($url). &quot;&amp;login=&quot;. BIT_LY_USERNAME. &quot;&amp;apiKey=&quot;. BIT_LY_API_KEY. &quot;&amp;format=xml&quot;;

$this-&gt;curl-&gt;exec($api_call);
$http_code=$this-&gt;curl-&gt;getHttpCode();
if ($http_code==200||$http_code==201) {
$response=$this-&gt;curl-&gt;getExecResponse();
$xml=new SimpleXmlElement($response);
return (string)$xml-&gt;results-&gt;nodeKeyVal-&gt;shortUrl;
}
else return false;
}
}
?&gt;
</code></pre>

Advertisements
This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s