Code: Slinky: A PHP URL-shortener/lengthener library

Slinky is a relatively simple PHP library which allows you to go back and forth between “long” and “short” versions of a URL using common URL shortening services such as Bit.ly, Tr.im and of course TinyURL. It attempts to use the public API of a service where possible, or will also just follow a short URL to get the long version. If you’re going to use it, I strongly suggest you implement some sort of local caching rather than relying on external HTTP requests every time you want to find out the long version of a short URL.

Download   Slinky (6.4 KiB, 1,671 hits)

Features

  • Really easy to use
  • Out of the box support (shortening and lengthening) for some of the most popular services, currently: TinyURL, Bit.ly, Tr.im, Is.gd, Fon.gs, Micurl, ur1.ca, Ptiturl, TightURL and Snipr.
  • Auto-detection of short URLs and automatic use of the most efficient way available to get a long version.
  • Includes a “cascade” feature, where you can specify a series of shortening services and Slinky will automatically try each one until it gets a successful response (in the case of timeouts, site outages etc)
  • Super-simple to extend for your own favorite service, just create a small new PHP class and you’re ready to go.
  • Licensed under the very liberal BSD license.

Requirements

  • PHP5+
  • cURL installed and operational
  • json_encode/_decode enabled for services which use them (not required for core operations)

Usage

There are examples in the code comments, but very basically:

// Get a short version of http://dentedreality.com.au/
$slinky = new Slinky( 'http://dentedreality.com.au/' );
$short = $slinky->short();
echo $short;

// And then lengthen it back out again
echo $slinky->long( $short );

Comments