/ Published in: PHP
Use some of the external (third-party) link shorteners to shorten your URLs.
Edit 11/17/10: Added Brif.us shortener
Edit 11/17/10: Added Brif.us shortener
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // ============================= // Shorteners Script // ============================= // // Shorteners Script currently supports following shortener services // - TinyURL (use as: tinyurl) // - BitLy (use as: bitly) // - SuPr (use as: supr) // - IsGd (use as: isgd) // - L4uIn (use as: l4uin) // - ToLy (use as: toly) // - Adfly (use as: adfly) // - KwnMe (use as: kwnme) // - BrifUs (use as: brifus) // // Usage: // http://www.example.com/short.php?shortener=<SHORTENER SERVICE PROVIDER>&longurl=<URL TO SHORTEN> // ================================================================================================== // Get the passed arguments $shortener = $_GET['shortener']; $passedurl = $_GET['longurl']; // Determine if the passed long URL has the 'http://' at the start $passedurl = 'http://' . $passedurl; } // Determine which function to call if ($shortener == 'tinyurl') { $shorturl = shortTinyURL($passedurl); } elseif ($shortener == 'bitly') { $shorturl = shortBitly($passedurl); } elseif ($shortener == 'supr') { $shorturl = shortSupr($passedurl); } elseif ($shortener == 'isgd') { $shorturl = shortIsgd($passedurl); } elseif ($shortener == 'l4uin') { $shorturl = shortL4uin($passedurl); } elseif ($shortener == 'toly') { $shorturl = shortToly($passedurl); } elseif ($shortener == 'adfly') { $shorturl = shortAdfly($passedurl); } elseif ($shortener == 'kwnme') { $shorturl = shortKwnme($passedurl); } elseif ($shortener == 'brifus') { $shorturl = shortBrifus($passedurl); } else { $shorturl = 'You need to use a valid shortener service.'; } // Print the result // or you can do what ever you like with the result (shortened URL) echo $shorturl; // TinyURL shortener function shortTinyURL($ToConvert) { return $short_url; } // Bit.ly shortener function shortBitly($ToConvert) { $bitlylogin = 'YOUR_USER_NAME'; $bitlyapikey = 'YOUR_API_KEY'; $bitlyurl = file_get_contents('http://api.bit.ly/shorten?version=2.0.1&longUrl=' . $ToConvert . '&login=' . $bitlylogin . '&apiKey=' . $bitlyapikey); $bitlyerror = $bitlycontent['errorCode']; $short_url = $bitlycontent['results'][$ToConvert]['shortUrl']; return $short_url; } // Su.pr shortener function shortSupr($ToConvert) { return $short_url; } // Is.gd shortener function shortIsgd($ToConvert) { return $short_url; } // L4u.in shortener function shortL4uin($ToConvert) { $short_url = file_get_contents('http://www.l4u.in/?module=ShortURL&file=Add&mode=API&url=' . $ToConvert); return $short_url; } // To.ly shortener function shortToly($ToConvert) { return $short_url; } // Adf.ly shortener function shortAdfly($ToConvert) { $APIKey = 'YOUR_API_KEY'; $UserID = 'YOUR_USER_ID'; $ShortType = 'int'; // or 'banner' $short_url = file_get_contents('http://adf.ly/api.php?key=' . $APIKey . '&uid=' . $UserID . '&advert_type=' . $ShortType . '&url=' . $ToConvert); return $short_url; } // Kwn.me shortener function shortKwnme($ToConvert) { return $short_url; } // Brif.us shortener function shortBrifus($ToConvert) { $short_url = file_get_contents('http://brif.us/api.php?action=shorturl&format=simple&url=' . $ToConvert); return $short_url; } ?>