Dennis Madsens Blog

04 Jan, 2010

Bit.ly API class for PHP

Posted by: Dennis Madsen In: PHP

I’ve created a Bit.ly class for PHP, which lets you easily cooperate with the Bit.ly API. Bit.ly offers you a service where everyone can generate a small, unique URL from a large and unhandy URL.

Requirements

Example

Before you download the source code I would like to start by showing you how to use the Bit.ly class. Here is a small example:

<?php
require("bitly.class.php");

// Creating an instance of the bitly-class
$bitly = new Bitly();

// Setting the authentication for your Bit.ly user account. http://bit.ly/account/your_api_key
$login = "yourLogin";
$apiKey = "yourApiKey";
$bitly->setAuthentication($login, $apiKey);

// Getting a short URL by Bit.ly
echo $bitly->getShortURL("http://www.dennismadsen.com")."";
// result: http://bit.ly/8rPmdz

// Expand a short Bit.ly URL to the original URL
echo $bitly->getLongURL("http://bit.ly/7px4Q2")."";
// result: http://www.google.com/

// Get number of total click on a short Bit.ly URL
echo $bitly->getClicks("http://bit.ly/7px4Q2")."";
// result: E.g. 47000

// Get number of click by current bit.ly user on a short Bit.ly URL
echo $bitly->getClicksByMe("http://bit.ly/7px4Q2");
// result: E.g. 2
?>

All you have to do is to insert your Bit.ly username and your api Key. See http://bit.ly/account/your_api_key.

Download

Download Download Bitly.class.php (v. 1.0.1)

Please feel free to leave a comment below.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • email

Like it? Tweet it!

4 Responses to "Bit.ly API class for PHP"

1 | How to build a web service in 8 hours

February 21st, 2010 at 11:51

Avatar

[...] the URL shortener. I used this Bit.ly PHP class by Dennis Madsen to makes things even [...]

2 | Steve Terjeson

March 24th, 2010 at 16:57

Avatar

Thanks Dennis, have you added any of the other bitly functions to the class lately, stats, etc?

3 | Dennis Madsen

March 24th, 2010 at 22:42

Avatar

Hello Steve,

I’ve just updated the bitly class to version 1.0.1.
Now it contains two new methods called getClicks and getClicksByMe. You can see how they can be used in the example code. I hope it was what you were looking for!

Best Regards,
Dennis Madsen

4 | Hugh Durkin

July 21st, 2010 at 15:36

Avatar

Thanks for such a great piece of code.

After messing around with a different class (which didn’t work properly), this works a dream.

You rock!

Hugh

Comment Form

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Categories

Twitter

    Tags

    Archives