BitLy (bit.ly) PHP Class – Shorten and Expand URLs (and Hashes) with BitLy API
BitLy (bit.ly) is a service which allows users to shorten, expand, share and track URLs (links). bit.ly can be accessed through bit.ly website and a robust and open API.
Example:
Shorten http://www.if-not-true-then-false.com/ url -> http://bit.ly/8cZ1fb
Expand http://bit.ly/8cZ1fb url -> http://www.if-not-true-then-false.com/
This post deals with bit.ly API usage with a simple PHP Class. This PHP class allows to shorten normal urls, expand bit.ly urls and expand bit.ly hashes.
BitLy PHP Class
<?php class BitLy { private $break; private $api_version; private $format; private $login; private $apikey; // Constructor for BitLy class public function __construct($login, $apikey) { // Browser output then use "<br />" // Command line output use "\n" $this->break = "\n"; $this->api_version = "2.0.1"; $this->format = "json"; $this->login = $login; $this->apikey = $apikey; } // Shorten given url and returns shortened url public function shortenUrl($url) { $shortened_url = ""; $encoded_url = urlencode($url); $bitly_url = "http://api.bit.ly/shorten?" . "version=" . $this->api_version . "&format=" . $this->format . "&longUrl=" . $encoded_url . "&login=" . $this->login . "&apiKey=" . $this->apikey; $content = file_get_contents($bitly_url); try { $shortened_url = $this->parseContent($content, $url); } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . $this->break; exit; } return $shortened_url; } // Expand given url and returns expanded url public function expandUrlByUrl($url) { $expanded_url = ""; $hash = $this->parseBitlyUrl($url); $expanded_url = $this->expandUrlByHash($hash); return $expanded_url; } // Expand given hash and returns expanded url public function expandUrlByHash($hash) { $expanded_url = ""; $bitly_url = "http://api.bit.ly/expand?" . "version=" . $this->api_version . "&format=" . $this->format . "&hash=" . $hash . "&login=" . $this->login . "&apiKey=" . $this->apikey; $content = file_get_contents($bitly_url); try { $expanded_url = $this->parseContent($content, $hash); } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . $this->break; exit; } return $expanded_url; } // Parse Bitly url returns bitly hash private function parseBitlyUrl($url) { $parsed_url = parse_url($url); return trim($parsed_url['path'], "/"); } // Parse Content from Bitly API private function parseContent($content, $key) { // Decode json to array $content = json_decode($content, true); // Check errors if ($content['errorCode'] != 0 || $content['statusCode'] != "OK") { throw new Exception($content['statusCode'] . ": " . $content['errorCode'] . " " . $content['errorMessage']); } // Return right url or throw exception if not set if (isset($content['results'][$key]['longUrl'])) { return $content['results'][$key]['longUrl']; } else if (isset($content['results'][$key]['shortUrl'])) { return $content['results'][$key]['shortUrl']; } else { throw new Exception("ERROR. URL not found: " . $key); } } } ?> |
BitLy PHP Class usage
<?php require_once("BitLy.php"); $bitly = new BitLy("bitlyapidemo", "R_0da49e0a9118ff35f52f629d2d71bf07"); $url = "http://www.if-not-true-then-false.com/"; $short_url = $bitly->shortenUrl($url); echo "Shorten " . $url . " url -> " . $short_url . "\n"; echo "Expand " . $short_url . " url -> " . $bitly->expandUrlByUrl($short_url) . "\n"; echo "Expand 8cZ1fb hash -> " . $bitly->expandUrlByHash("8cZ1fb") . "\n"; ?> |
Output
$ php test_bitly_class.php Shorten http://www.if-not-true-then-false.com/ url -> http://bit.ly/8cZ1fb Expand http://bit.ly/8cZ1fb url -> http://www.if-not-true-then-false.com/ Expand 8cZ1fb hash -> http://www.if-not-true-then-false.com/ |
3 Comments
Leave a Comment
Trackbacks/Pingbacks
- BitLy (bit.ly) PHP Class – Shorten and Expand URLs (and Hashes … | Coder Online - [...] The rest is here: BitLy (bit.ly) PHP Class – Shorten and Expand URLs (and Hashes … [...]


Thanks a lot, great class and using this in an upcoming website. Cheers.
Thank you for your feedback! Glad to hear that you like this class, and even happier to hear that you are using this class in the future project. :)
Nice Class..Thanx