Services_ShortURL is an abstract interface to the plethora of short/tiny/miny
URL services written in PHP5 for PEAR. It allows you to easily create or expand
short URL’s. In addition to expanding and shortening short URL’s it has helper
functions for detecting what type of short URL service a URL belongs to and
extracting short URL’s and expanding them from blogs of text.
- bit.ly
- Digg
- is.gd
- short.ie
- su.pr
- TinyURL
- tr.im
<?php
$url = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html';
require_once 'Services/ShortURL.php';
$api = Services_ShortURL::factory('TinyURL');
$short = $api->shorten($url);
// I'm short!
var_dump($short);
?>
<?php
$url = 'http://tinyurl.com/jumvn';
require_once 'Services/ShortURL.php';
$api = Services_ShortURL::factory('TinyURL');
$long = $api->expand($url);
// Should be http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
var_dump($short);
?>
If you have a blob of text that may or may not contain short URLs in it, you can easily extract them with the Services_ShortURL::extract()
method. This method looks through the text for known short URLs, extracts them, expands them and then returns an array with a key/value pair of short/long URLs.
<?php
$txt = 'Hey check out http://tinyurl.com/jumvn and http://bit.ly/EojDf if you like politics.';
require_once 'Services/ShortURL.php';
$urls = Services_ShortURL::extract($txt);
// Will be a hash with short => long URls in it.
print_r($urls);
?>
You can post short messages to services such as Twitter and Facebook using the post and schedule methods.
<?php
$msg = 'Hi Mom! http://www.imdb.com/name/nm0000571/';
require_once 'Services/ShortURL.php';
$api = Services_ShortURL::factory('Supr');
$shortmsg = $api->post($url);
// Should be "Hi Mom! http://su.pr/2oKPAf"
var_dump($shortmsg);
$msg = 'Hi Mom! http://www.imdb.com/name/nm0000571/';
require_once 'Services/ShortURL.php';
$api = Services_ShortURL::factory('Supr');
$when = time() + (24*60*60); // now + 24h
$shortmsg = $api->schedule($url, $when);
// Should be "Hi Mom! http://su.pr/2oKPAf"
var_dump($shortmsg);
?>