Skip to content

Slinky

Slinky allows you to go back and forth between "long" and shortened URLs using popular URL shortening services.

Slinky assumes you have cURL installed and working, and requires the JSON extension installed if you're working with a service that uses JSON.

Slinky will ONLY work with PHP5+

It supports some of the more popular services, with easy extensibility for adding your own relatively easily. It defaults to using TinyURL for shortening URLs. If you want to use some of the other services, you need to set some configuration options before actually shortening/lengthening URLs. I'd strongly suggest that you cache results using memcached, a local DB or whatever to avoid having to hit APIs etc every time you encounter a URL.

Slinky supports shortening, and auto-detection (for lengthening URLs) using these services: - Bit.ly - Tr.im - TinyURL - Is.Gd - Fon.gs - Micurl.com - ur1.ca - Ptiturl - Tighturl - 2tu.us - Snipr / Snipurl / Snurl.com / Sn.im

To use Slinky:

$slinky = new Slinky( 'http://dentedreality.com.au/' ); - Creates a new Slinky instance, will default to using TinyURL for ->short();

$slinky = new Slinky( 'http://dentedreality.com.au', new Slinky_Bitly() ); - Creates new Slinky, forces use of Bit.ly for ->short();

$slinky = new Slinky( 'http://dentedreality.com.au/' ); echo $slinky->short(); - echos the short version of http://dentedreality.com.au/ (default to TinyURL)

$slinky = new Slinky( 'http://tinyurl.com/jw5sh' ); echo $slinky->long(); - echos out the long version of http://tinyurl.com/jw5sh (which should be http://dentedreality.com.au/)

$slinky = new Slinky( 'http://dentedreality.com.au/' ); echo $slinky->long(); - Attempts to lengthen the URL, but will not auto-detect which service it is so it will just output the original URL. Useful for always attempting to lengthen any URL you come across (fails gracefully)

$slinky = new Slinky( 'http://dentedreality.com.au/' ); $slinky->set_cascade( array( new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); echo $slinky->short(); - Uses the powerful cascade mode to make sure that we get a short URL from Tr.im, Is.Gd or TinyURL (in that order).

See specific service class definitions below for examples of how to use them, as some services allow (or require) additional properties before you can use them (for authentication etc).

To use a different service with Slinky, just create your own class and extend Slinky_Service(). Make sure you implement url_is_short(), url_is_long(), make_short() and make_long(). If you need to GET or POST a URL, you can use ->url_get() and ->url_post(), which your class will have inherited.

  • Full name: \Slinky

Properties

url

public $url

service

public $service

cascade

public $cascade

Methods

__construct

public __construct(mixed $url = false, mixed $service = false): mixed

Parameters:

Parameter Type Description
$url mixed
$service mixed

set_service

Specify which URL Service to use

public set_service(\Slinky_Service $service = false): void

Parameters:

Parameter Type Description
$service \Slinky_Service Packaged or custom Service object

set_cascade

If you pass an array of Slinky_Service objects to this method, they will be used in order to try to get a short URL, so if one fails, it will try the next and so on, until it gets a valid short URL, or it runs out of options.

public set_cascade(array $services = false): mixed

Parameters:

Parameter Type Description
$services array List of Slinky_Service objects as an array

set_service_from_url

Guess the URL service to use from known domains of short URLs

public set_service_from_url(string $url = false): mixed

Parameters:

Parameter Type Description
$url string

short

Take a long URL and make it short. Will avoid "re-shortening" a URL if it already seems to be short.

public short(string $url = false): \The

Parameters:

Parameter Type Description
$url string Optional URL to shorten, otherwise use $this->url

Return Value:

short version of the URL


long

Take a short URL and make it long ("resolve" it).

public long(string $url = false): \A

Parameters:

Parameter Type Description
$url string The short URL

Return Value:

long URL



Automatically generated on 2025-03-18