Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Create Bit.ly Short URLs Using PHP: API Version 3

13 Responses »

Bit.ly is a great URL shortening service. I love their reliability, shortness of the URL, and the information they provide about a given URL. Recently Bit.ly updated their API to version 3 so I thought I'd update my original Bit.ly post. Here's how you can create short URLs and expand short URLs using Bit.ly.

The PHP

/* returns the shortened url */
function get_bitly_short_url($url,$login,$appkey,$format='txt') {
	$connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
	return curl_get_result($connectURL);
}

/* returns expanded url */
function get_bitly_long_url($url,$login,$appkey,$format='txt') {
	$connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
	return curl_get_result($connectURL);
}

/* returns a result form url */
function curl_get_result($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

/* get the short url */
$short_url = get_bitly_short_url('http://davidwalsh.name/','davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

/* get the long url from the short one */
$long_url = get_bitly_long_url($short_url,'davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

All you really need to is pass your appkey and login (you must sign up for their API service), the long or short URL, and the format which you'd like the result to be returned in. If you just want a simple URL with no other information, use the default "txt" format. Retrieving the XML or JSON formats will provide you more information about the URL.

Bit.ly is awesome. I mean, Twitter uses them -- what more of an endorsement would you need.

Discussion

  1. April 8, 2010 @ 7:59 pm

    Thanks for the tip!

    To simplify a little bit your php code, you can use file_get_contents() http://php.net/manual/en/function.file-get-contents.php) instead of curl.

  2. April 9, 2010 @ 6:14 am

    Why not just use file_get_contents? It’s built in to PHP and doesn’t need cURL to be installed (not all hosts would have cURL). Simply change “return curl_get_result(” to “return file_get_contents(” and remove the whole curl_get_result function :)

  3. April 9, 2010 @ 8:14 am

    @Saad, @Daniel15: When the host has allow_url_fopen disabled, you can’t use file_get_contents on remote files – which is why I guess David has used cURL. Nowadays, pretty much every host has cURL installed as it’s such a commonly used package.

  4. April 9, 2010 @ 8:19 am

    @Saad, @Daniel15, @Michael: cURL is a billiion times faster than file_get_contents.

  5. April 9, 2010 @ 8:25 am

    @Michael: Barely anyone has allow_url_fopen disabled now, since allow_url_include was added in PHP 5.2.0. The main reason for hosts to disable it was that it could include remote files, but now that’s a separate setting, so there’s no reason to disable it.

    @David: Oh, really? I didn’t know that. Do you have benchmarks for it?

  6. April 9, 2010 @ 8:28 am

    @Daniel15: Ah, I’ve not used a shared host since before 5.2 was released, but I know there were a few that had it disabled before then.

    @David: I’d be interested in seeing some benchmarks too if you have them handy :)

  7. April 9, 2010 @ 8:31 am

    @Daniel15, @Michael: You can see benchmarks here:

    http://stackoverflow.com/questions/555523/filegetcontents-vs-curl-what-has-better-performance

    No question that cURL is a faster by a longshot.

  8. April 9, 2010 @ 9:02 am

    I just benchmarked them myself and couldn’t see much of a difference:

    cURL:
    0.23367691040039
    0.22581005096436
    0.21861100196838
    0.2252950668335
    0.21909284591675
    0.24202489852905
    0.23065900802612
    0.21276307106018
    0.22140598297119
    0.21932482719421

    file_get_contents:
    0.22237586975098
    0.21461701393127
    0.22064304351807
    0.22495198249817
    0.21172213554382
    0.22352600097656
    0.21351790428162
    0.22140502929688
    0.21303987503052
    0.2165310382843

    Code I used is here: http://pastebin.ws/awugtf

  9. whitey
    April 10, 2010 @ 9:30 pm

    While cURL may be faster, not every host has cURL installed, and you should never assume the user will have anything but PHP installed with the default settings.

    Following this rule i’d still opt to using file_get_contents() rather than cURL, just for the compatability.

    But nonetheless, good post. :)

  10. April 12, 2010 @ 6:28 am

    Hi David,

    A few months ago i posted on my Snipplr account how to do the same with Mootools:

    http://snipplr.com/view/29387/connect-to-bitly-api–with-mootools/

    Cheers,
    Stephane.

  11. August 30, 2010 @ 6:15 pm

    I love how everyone is fighting and taking the time to benchmark fucking php functions, but no one has released a dual optioned script.. :/

    Some host’s disable both for security reasons.. If you ask me, PHP isn’t exactly the best way to do the script in the first place.

  12. August 30, 2010 @ 7:11 pm

    Why would PHP not be the best way to do something like this? What would you do instead?

    And here’s a dual-optioned script… Uses cURL if available, otherwise file_get_contents:

    function get_url($url)
    {
    if (!function_exists('curl_init'))
    {
    return file_get_contents($url);
    }

    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

  13. August 30, 2010 @ 7:13 pm

    The <code> tag broke my indentation though. :(

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!