28/12
2016
Minimum viable URL shortener class
Do you want to encode URLs like tinyurl.com does?
This is helper tool for you., (*1)
Urly uses PDO interface to store URLs. Just refer to your existing connection., (*2)
Optionally you can specify table name, key encode base and XOR mask., (*3)
Access methods are quite obvious: set($url)
and get($key)
., (*4)
To install with composer:, (*5)
composer require artoodetoo/urly
Required table structure:, (*6)
CREATE TABLE `urly` ( `id` int(10) NOT NULL AUTO_INCREMENT, `url` varchar(1000) NOT NULL, PRIMARY KEY (`id`) )
Save URL and het encoded key:, (*7)
$db = new \PDO( 'mysql:dbname=homestead;host=127.0.0.1;charset=utf8', 'homestead', 'secret' ); // Set 62 base to get alfa-numeric key in both cases and // some magic number to make key sequence be less predictable $shortener = new \R2\Utility\Urly($db, 'my_urly', 62, 990749); $key = $shortener->set('http://localhost/test.txt'); echo 'http://go.to/'.$key."\n"; // Something like 'http://go.to/49Jz'
Get URL by key:, (*8)
echo $shortener->get('49Jz'); // Saved URL or empty string if not found
The Urly is open-source software, licensed under the MIT license, (*9)