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

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.


Comments

  1. Saad

    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.

    • Dawson

      This may be true but not all servers allow file_get_contents() to be run, making CURL the real only solution.

  2. Daniel15

    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. Michael

    @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. David Walsh

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

  5. Daniel15

    @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. Michael

    @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. David Walsh

    @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. Daniel15

    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

    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. Stephane Pericat

    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. Xanza

    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. Daniel15

    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. Daniel15

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

  14. Leonardo Rothe

    I gave your code a nice OO suite.


    class Bitly {

    private $login;
    private $appkey;

    function __construct($login, $appkey) {
    $this->login = $login;
    $this->appkey = $appkey;
    }

    function shorten($url) {
    return $this->curl_get_result("http://api.bit.ly/v3/shorten?login={$this->login}&apiKey={$this->appkey}&uri=".urlencode($url)."&format=txt");
    }

    function expand($url) {
    return $this->curl_get_result("http://api.bit.ly/v3/expand?login={$this->login}&apiKey={$this->appkey}&shortUrl=".urlencode($url)."&format=txt");
    }

    private 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;
    }

    }

    $bitly = new Bitly("login", "apiKey");

    $url = $bitly->shorten("url");

    Hope you like it.

  15. misterhaan

    thanks for posting this — i’m trying to use the bit.ly api to shorten urls before posting on twitter through their api. i was looking at the documentation at http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/shorten and it said to use longUrl, x_login, and x_apiKey instead of uri, login, and apiKey. when i changed to the parameters you use here it started working. now i just have to hook in to the parts of my site that should update twitter and i’ll have finished my first project of integrating with another site!

  16. Wes

    If you don’t have cURL installed, install it. If you don’t have access to install cURL and its not already there, find a better host. file_get_contents() is slow and if you actually get a moderate amount of traffic volume, don’t use file_get_contents. Programmers, don’t be lazy, use cURL. David, nice post.

  17. WilleNewMedia

    Awesome, thank you so much, David!

  18. Christopher Stevens

    This bit.ly script is a joy to use. Thanks so much for posting! :D

  19. Christopher Stevens

    This bitly script was a joy to use. Thanks so much for posting! :)

  20. FrankF

    Question. I have a large homepage with 12 news items and I want a fast bit.ly link with every news item. But after one link I receive the erorr:
    Fatal error: Cannot redeclare get_bitly_short_url() (previously declared(…)

    What can i do?

  21. Puneet

    Hi, Thanks for this code. Just to add some. you can use wp_remote_fopen if you are running a WP blog. It will try with fopen first to retrieve the remote url and if it is not present it will switch over to curl.

  22. Koen Heltzel

    If you get the MISSING_ARG_APIKEY error using the above script, you have probably used the “copy” button to copy it to your clipboard, which adds html entity encoding. Replace & with & in the url’s and the error should go away.

  23. Koen Heltzel

    Of course I meant to say: replace “& amp ;” with “&”

  24. Tiago Celestino

    My code: http://pastebin.com/CYzuThRV

    The result in browser is: bool(false)

    what’s wrong?


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: