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('https://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.
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.
This may be true but not all servers allow file_get_contents() to be run, making CURL the real only solution.
file_get_contents() slows things down.
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 :)
cURL is about 10x faster.
@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.
@Saad, @Daniel15, @Michael: cURL is a billiion times faster than file_get_contents.
@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?
@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 :)
@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.
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
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. :)
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.
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.
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
:The <code> tag broke my indentation though. :(
I gave your code a nice OO suite.
Hope you like it.
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!
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.
Awesome, thank you so much, David!
This bit.ly script is a joy to use. Thanks so much for posting! :D
This bitly script was a joy to use. Thanks so much for posting! :)
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?
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.
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.
Of course I meant to say: replace “& amp ;” with “&”
My code: http://pastebin.com/CYzuThRV
The result in browser is: bool(false)
what’s wrong?
The problem is resolved. The problem is my local server.
I’m getting an ugly line return after outputting the function. Can’t work out what’s causing it? Any hints? :)
Ok, so I’ve worked out where it’s come from. The file that bit.ly outputs has the data on the first line, but then also outputs a blank second line.
Any ideas how to remove it?
Solved the problem. I ended up calling it as XML rather than TXT.
Then for that to work I made a tiny edit in the function so we don’t need CURL.
Might help someone out there!
Thank you so much! it such big help and very direct for a noob like me! :)
Thank you!!
In case this helps someone else out – I kept confusing my generic oauth token (link on the main API docs page) for the API_KEY
You can easily find your API Key here:
https://bitly.com/a/your_api_key
hey … look this project for make an API .. is cool, http://codecanyon.net/item/our-api-server/5679947
Thanks for the example…it helped alot! It is generating the bitly, but it is wrapped with a
string(22) “http://bit.ly/1xL48Nx”. Kind of a newbie…how do I get rid of the string(22)””? Any help would be great…thanks.
@kelly:
return trim(curl_get_result($connectURL));
@dikau Why do you need the trim function? or I mean why is there a line break after the URL? does anyone know?
You Rock , Man
It worked on the 1st time.
One question : Is there a limitation in the number of bitly one can generate per day ??
What if I have multiple users using my code ??( what will create a big number of short links )
Fatal error: Cannot redeclare make_bitly_url() (previously declared in C:\code directory:3) in C:\code directory on line 23