PHPShortener

PHPShortener is a class that makes it easy to encode and decode short URLs. It can turn http://google.com into http://is.gd/w and viceversa. Currently it supports the 5 most popular shortening services.

Features

  • Easy to use

    require('phpshortener.class.php');
    // initialize it
    $s = new PHPShortener();
    // encode a long url
    $shorturl = $s->encode('http://devthought.com/projects/php/phpshortener/', 'is.gd');
    // decode a short url
    $longurl = $s->decode('http://tr.im/jBBp');
  • Flexible, easy to extend with new services
  • Auto detects the service in the URL to decode
  • Tiny, but well written and commented.
  • PHP5 / PHP4 compatible (although only tested in PHP5 so far)
  • Works with cURL or fopen wrappers, without configuration.
  • Decodes URLs by inspecting the redirection headers, which means:
    • It decodes fast
    • Setting up a new service only requires writing the encode function

Supported services

The list is growing!

Changelog

  • 0.1: initial release
  • 0.2
    • DiggBar added
    • encode now returns the passed URL if shortening failed
    • fetch cURL sends a user agent header.
  • 0.3: u.nu added

Notes

  • bit.ly requires to set up a login and apikey. Signup and edit services/bit.ly.php
  • tr.im might optionally use an apikey, if you need to encode too many shortened URLs per day. If so, edit services/tr.im.php