20
Feb
2015

Generate bit.ly Short URLs with PHP

posted Friday, February 20th 2015 at 8:40 AM by

Generate bit.ly Short URLs with PHP

Need to shorten URLs automatically for your website (or automated Twitter feed)? No problem, bit.ly has a neat API that you can connect to via PHP!

First Steps

bit.ly is a tool built around managing links, specifically, generating short links. For example, I created a bitlink for http://www.adamkonieska.com, which bit.ly shortened to http://bit.ly/1zkZCdo. If you don't have a bit.ly account, start by heading over and signing up now!

Once you've got your account set up, the next step is to grab your API keys. Start by creating a new OAuth application at bit.ly.

Create a new bit.ly OAuth Application

Next, you'll receive an email with the authentication link. Click the link, and finish the process by filling out the information for your application. Once you're done, hit Create Your App.

Create a new bit.ly OAuth Application

Finally, you'll want to note your API keys, the Client ID and Client Secret. You will also need to generate an Access Token. Keep all this info handy, we'll need it again when we start with the PHP.

bit.ly OAuth Application API Keys

You'll also need your bit.ly account key which can be found on the advanced account settings page.

bit.ly OAuth Application Account Info

Using bit.ly with PHP

Now that you've got your bit.ly application ready, the next step is to connect to it using PHP. Fortunately, most of the legwork has been done for you by BitlyPHP. You can download the BitlyPHP toolkit over at GitHub. Once you've got the bitly.php file, put it in a secured, non-public, directory on your server like /includes/.

Next, add your keys to BitlyPHP by updating the bitly.php file. You'll need to update lines 16, 21, 26, and 31 with the information and API keys we gathered earlier.

bit.ly Shorten URL with PHP

Shortening a URL with bit.ly and PHP

Now that we have everything set up, we can use BitlyPHP to shorten URLs via PHP. Here is some example code:


require_once("bitly.php"); //make sure to update with your path
function shortenURL($fullUrl) {
    $accessToken = "your_access_token_from_bitly"; //replace this with your access token
    return(bitly_v3_shorten($fullUrl,$accessToken));
}


echo(shortenURL("http://www.adamkonieska.com/archives/generate-bitly-short-urls-with-php/"));

The function above would output a bit.ly short URL, something like http://bit.ly/1DShxwc.

Final Thoughts

Bit.ly is a great tool for shortening URLs. Especially when being used with an automated Twitter feed via PHP.

Questions or problems with the code? Let me know in the comments!

Share This:

Tags:

Comments:

View (3) Comments Post a Comment
  • Replying to Adam Konieska on Generate bit.ly Short URLs with PHP







  • kirk ward
    Kirk Ward
    Monday, May 18th 2015 at 6:47 PM

    I'm trying to use the bitly.php as described, using the function and the example URL inside a Wordpress page. All goes well until I try to echo the bit.ly shortened link. The message I get is "Array."

    You can see my result at http://practicebuilderpublishing.com/code-test

    The URL above the bitly.php output is the typical dynamic URL I want to shorten, using bit.ly and bitly.php.

    Can you advise me as to where I have an error?

    Or, has bit.ly changed their api since the post was written?

    Many thanks
    Kirk Ward

    P.S. Are you the developer? (I have a hard time figuring simple things like this out. I'm starting to use age as an excuse.)

    • Replying to Kirk Ward







    • Adam Konieska
      AK
      Wednesday, May 20th 2015 at 3:12 PM

      Hey Kirk! The first thing I'd check would be the API keys and the bit.ly account.... but it looks like you've already got it working from our email exchange. Nice job! If you need anything else, just drop me a line!

      • Replying to AK







  • ramon michael
    Ramon Michael
    Thursday, December 17th 2015 at 11:44 AM

    Thanks for the very helpful tutorials Adam, this one as well as the auto posting to Twitter one. Both are very well explained with nice screenshots.

    • Replying to Ramon Michael