URL Shortening atau layanan pemendek URL (link) adalah layanan yang memberikan sebuah link pendek dari suatu halaman web atau aplikasi web. Pertama kali dipopulerkan pada tahun 2001, dan mulai tersedia ke publik luas pertama kali oleh TinyURL pada tahun 2002. Sekarang telah lebih dari 100 macam penyedia terekspos ke publik, dan kepopulerannya kembali terangkat dengan hadirnya bit.ly pada tahun 2009 dan segera menjadi pemain utama.
Layanan pemendek URL ini sangat populer digunakan sebagai penghemat jumlah karakter pada layanan SMS atau social networking yang membatasi jumlah karakter seperti Twitter.
Belajar Membuat URL Shortening dengan PHP
Untuk membuat URL Shortening, kita dapat memanfaatkan class PHP berikut:
[sourcecode language=”php”]
<?php
/* Abstract class ShortUrl */
abstract class ShortUrl {
protected $data;
abstract protected function api();
abstract protected function execute($url);
public function getTinyUrl() {
return $this->data;
}
protected function get($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
if(empty($data)) {return ‘Error retrieving data. Try again.’;}
else
{return $data;}
}
final protected function validUrl($longurl){
if(!preg_match(‘|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i’, $longurl)) {
throw new Exception("The url is not valid. Please try again.");
}
}
}
/* Concret class TinyUrl */
class TinyUrl extends ShortUrl {
private $url;
public function __construct($longurl) {
$this->validUrl($longurl);
$this->url = urlencode($longurl);
$apiurl = sprintf($this->api(),$this->url);
$this->execute($apiurl);
}
public function execute($url) {
$this->data = $this->get($url);
}
public function api() {
return "http://tinyurl.com/api-create.php?url=%s";
}
}
/* Concret class Bitly */
class Bitly extends ShortUrl {
private $longurl;
public function __construct($longurl,$user,$api) {
$this->validUrl($longurl);
$this->longurl = urlencode($longurl);
$apiurl = sprintf($this->api(),$this->longurl,$user,$api);
$this->execute($apiurl);
}
public function execute($url) {
$content = $this->get($url);
$xml = simplexml_load_string($content);
$shortUrl = $xml->results->nodeKeyVal->shortUrl;
$this->data = $shortUrl;
}
public function api() {
return "http://api.bit.ly/shorten?version=2.0.1&longUrl=%s&login=%s&apiKey=%s&format=xml";
}
}
/* Concret class Trim */
class Trim extends ShortUrl {
private $longurl;
public function __construct($longurl) {
$this->validUrl($longurl);
$this->longurl = urlencode($longurl);
$apiurl = sprintf($this->api(),$this->longurl);
$this->execute($apiurl);
}
public function execute($url) {
$content = $this->get($url);
$xml = new simpleXMLElement($content);
$shortUrl = $xml->url;
$this->data = $shortUrl;
}
public function api() {
return "http://api.tr.im/v1/trim_url.xml?url=%s";;
}
}
/* Concret class Isgd */
class Isgd extends ShortUrl {
private $longurl;
public function __construct($longurl) {
$this->validUrl($longurl);
$this->longurl = urlencode($longurl);
$apiurl = sprintf($this->api(),$this->longurl);
$this->execute($apiurl);
}
public function execute($url) {
$content = $this->get($url);
$this->data = $content;
}
public function api() {
return "http://is.gd/api.php?longurl=%s";
}
}
/* Concret class Unu */
class Unu extends ShortUrl {
private $longurl;
public function __construct($longurl) {
$this->validUrl($longurl);
$this->longurl = urlencode($longurl);
$apiurl = sprintf($this->api(),$this->longurl);
$this->execute($apiurl);
}
public function execute($url) {
$content = $this->get($url);
$this->data = $content;
}
public function api() {
return "http://u.nu/unu-api-simple?url=%s";
}
}
/* Concret class Digg */
class Digg extends ShortUrl {
private $longurl;
public function __construct($longurl,$apikey) {
$this->validUrl($longurl);
ini_set(‘user_agent’,’Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6′);
$this->longurl = urlencode($longurl);
$apiurl = sprintf($this->api(),$apikey,$this->longurl);
$this->execute($apiurl);
}
public function execute($url) {
$xml = file_get_contents($url);
$data = preg_match(‘/short_url="(.*)"/isU’,$xml,$matches);
$this->data = $matches[1];
}
public function api() {
return "http://services.digg.com/url/short/create?type=xml&appkey=%s&url=%s";
}
}
/* class Factory using method Factory*/
class Factory {
public static function create($name, $url, $user=”, $key=”) {
switch(strtolower($name)) {
case ‘bitly’:
$obj = new Bitly($url,$user,$key);
break;
case ‘tinyurl’:
$obj = new tinyUrl($url);
break;
case ‘trim’:
$obj = new Trim($url);
break;
case ‘isgd’:
$obj = new Isgd($url);
break;
case ‘unu’:
$obj = new Unu($url);
break;
case ‘digg’:
$obj = new Digg($url,$key);
break;
}//end switch
return $obj;
}
}
/* How to Usage */
/*
//Holds in variable ‘$longUrl’ the below address post
$longUrl = ‘http://www.wait-till-i.com/2009/11/02/getting-a-list-of-flickr-photos-by-location-andor-search-term-with-a-yql-open-table/’;
//$ob = Factory::create(‘bitly’,$longUrl,"thinkphp","R_0cf8415f0c3f9fcfd867ce7613e43fc7");
//$ob = Factory::create(‘tinyurl’,$longUrl);
//$ob = Factory::create(‘trim’,$longUrl);
//$ob = Factory::create(‘isgd’,$longUrl);
//$ob = Factory::create(‘unu’,$longUrl);
//$ob = Factory::create(‘digg’,$longUrl,"thinkphp","http://thinkphp.ro");
try {
$ob = Factory::create(‘bitly’,$longUrl,"thinkphp","R_0cf8415f0c3f9fcfd867ce7613e43fc7");
echo$ob->getTinyUrl();
}catch(Exception $e) {echo$e->getMessage();}
$output = highlight_file($_SERVER[‘SCRIPT_FILENAME’]);
echo$output;
*/
?>
[/sourcecode]
dan gunakan sebagai berikut:
[sourcecode language=”php”]
<?php
if($_POST[‘longUrl’] && isset($_POST[‘longUrl’]) && $_POST[‘submit’]) {
require_once(‘shortUrl.factory.class.php’);
$longUrl = $_POST[‘longUrl’];
$site = $_POST[‘shorts’];
if($site === ‘bitly’) {
$user = "thinkphp";
$apikey = "R_0cf8415f0c3f9fcfd867ce7613e43fc7";
} else if($site == ‘digg’) {
$apikey = "http://thinkphp.ro";
}
try {
$ob = Factory::create($site,$longUrl,$user,$apikey);
$shortUrl = $ob->getTinyUrl();
}catch(Exception $e) {$shortUrl = $e->getMessage();}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Shorten, share and track your links</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css">
<style type="text/css">
html,body{background:#C5C7BE;margin:0;padding:0;}
#doc{background:#fff;border:1em solid #fff;}
h1{font-family:Calibri,Arial,Sans-serif;font-size:200%;margin:0 0 .5em 0; padding:0;}
h2{font-family:Calibri,Arial,Sans-serif;font-size:150%;margin:1em 0;padding:0;font-weight: bold}
select,input{border: 1px solid #ccc;padding: 4px;margin: 4px;font-size: 40px;text-align: center}
label{margin-right: 10px}
input{margin-left: 28px}
input:focus{background: #E2FFC1}
input[id=shortUrl]{margin-left: 17px;}
form{background: #F9FFD7;padding: 30px}
#shortUrl{background: #F9FF93;padding-left: 30px;margin-top: 10px}
#shortUrl input{background: #fff}
#shortUrl label{font-weight: bold}
#ft{ color:#ccc;margin: 4px;font-size: 10px;margin-top: 20px}
#ft a { color:#ccc;}
</style>
</head>
<body>
<div id="doc" class="yui-t7">
<div id="hd" role="banner"><h1>Shorten, share and track your links</h1></div>
<div id="bd" role="main">
<div class="yui-g">
<form action="<?php echo$_SERVER[‘PHP_SELF’];?>" method="POST">
<p><label for="longUrl">Long URL</label><input type="text" name="longUrl" id="longUrl"></p><p><label for="shorts">Select service</label><select id="shorts" name="shorts"><option value="bitly">bit.ly</option><option value="tinyurl">tinyurl.com</option><option value="trim">tr.im</option><option value="isgd">is.gd</option><option value="unu">u.nu</option><option value="digg">digg.com</option></select><input type="submit" id="shortner" name="submit" value="shortUrl"></p>
</form>
<div id="shortUrl">
<label for="shortUrl">Short URL: </label><input id="shortUrl" type="text" value="<?php if(isset($shortUrl)) echo$shortUrl; ?>" >
</div>
</div>
</div>
<div id="ft" role="contentinfo"><p>Created By <a href="http://thinkphp.ro">Adrian Statescu</a></p></div>
</div>
</body>
</html>
[/sourcecode]
Install pada domain web yang singkat, misalnya test.web.id, lempar.co.cc, dll. Class PHP diatas mendukung 5 layanan pemendek URL, seperti: bitly,tinyurl,trim,is.gd,u.nu,goo.gl.