Shortener Services


/ Published in: PHP
Save to your folder(s)

Use some of the external (third-party) link shorteners to shorten your URLs.

Edit 11/17/10: Added Brif.us shortener


Copy this code and paste it in your HTML
  1. <?php
  2. // =============================
  3. // Shorteners Script
  4. // =============================
  5. //
  6. // Shorteners Script currently supports following shortener services
  7. // - TinyURL (use as: tinyurl)
  8. // - BitLy (use as: bitly)
  9. // - SuPr (use as: supr)
  10. // - IsGd (use as: isgd)
  11. // - L4uIn (use as: l4uin)
  12. // - ToLy (use as: toly)
  13. // - Adfly (use as: adfly)
  14. // - KwnMe (use as: kwnme)
  15. // - BrifUs (use as: brifus)
  16. //
  17. // Usage:
  18. // http://www.example.com/short.php?shortener=<SHORTENER SERVICE PROVIDER>&longurl=<URL TO SHORTEN>
  19. // ==================================================================================================
  20.  
  21. // Get the passed arguments
  22. $shortener = $_GET['shortener'];
  23. $passedurl = $_GET['longurl'];
  24.  
  25. // Determine if the passed long URL has the 'http://' at the start
  26. if (strpos($passedurl, 'http://') !== false) {
  27. $passedurl = 'http://' . $passedurl;
  28. }
  29.  
  30. // Determine which function to call
  31. if ($shortener == 'tinyurl') {
  32. $shorturl = shortTinyURL($passedurl);
  33. } elseif ($shortener == 'bitly') {
  34. $shorturl = shortBitly($passedurl);
  35. } elseif ($shortener == 'supr') {
  36. $shorturl = shortSupr($passedurl);
  37. } elseif ($shortener == 'isgd') {
  38. $shorturl = shortIsgd($passedurl);
  39. } elseif ($shortener == 'l4uin') {
  40. $shorturl = shortL4uin($passedurl);
  41. } elseif ($shortener == 'toly') {
  42. $shorturl = shortToly($passedurl);
  43. } elseif ($shortener == 'adfly') {
  44. $shorturl = shortAdfly($passedurl);
  45. } elseif ($shortener == 'kwnme') {
  46. $shorturl = shortKwnme($passedurl);
  47. } elseif ($shortener == 'brifus') {
  48. $shorturl = shortBrifus($passedurl);
  49. } else {
  50. $shorturl = 'You need to use a valid shortener service.';
  51. }
  52.  
  53. // Print the result
  54. // or you can do what ever you like with the result (shortened URL)
  55. echo $shorturl;
  56.  
  57. // TinyURL shortener
  58. function shortTinyURL($ToConvert) {
  59. $short_url = file_get_contents('http://tinyurl.com/api-create.php?url=' . $ToConvert);
  60. return $short_url;
  61. }
  62.  
  63. // Bit.ly shortener
  64. function shortBitly($ToConvert) {
  65. $bitlylogin = 'YOUR_USER_NAME';
  66. $bitlyapikey = 'YOUR_API_KEY';
  67. $bitlyurl = file_get_contents('http://api.bit.ly/shorten?version=2.0.1&longUrl=' . $ToConvert . '&login=' . $bitlylogin . '&apiKey=' . $bitlyapikey);
  68. $bitlycontent = json_decode($bitlyurl,true);
  69. $bitlyerror = $bitlycontent['errorCode'];
  70. $short_url = $bitlycontent['results'][$ToConvert]['shortUrl'];
  71. return $short_url;
  72. }
  73.  
  74. // Su.pr shortener
  75. function shortSupr($ToConvert) {
  76. $short_url = file_get_contents('http://su.pr/api?url=' . $ToConvert);
  77. return $short_url;
  78. }
  79.  
  80. // Is.gd shortener
  81. function shortIsgd($ToConvert) {
  82. $short_url = file_get_contents('http://www.is.gd/api.php?longurl=' . $ToConvert);
  83. return $short_url;
  84. }
  85.  
  86. // L4u.in shortener
  87. function shortL4uin($ToConvert) {
  88. $short_url = file_get_contents('http://www.l4u.in/?module=ShortURL&file=Add&mode=API&url=' . $ToConvert);
  89. return $short_url;
  90. }
  91.  
  92. // To.ly shortener
  93. function shortToly($ToConvert) {
  94. $ch = curl_init();
  95. curl_setopt($ch, CURLOPT_URL, "http://to.ly/api.php?longurl=".urlencode($ToConvert));
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  97. curl_setopt($ch, CURLOPT_HEADER, 0);
  98. $shorturl = curl_exec ($ch);
  99. curl_close ($ch);
  100. return $short_url;
  101. }
  102.  
  103. // Adf.ly shortener
  104. function shortAdfly($ToConvert) {
  105. $APIKey = 'YOUR_API_KEY';
  106. $UserID = 'YOUR_USER_ID';
  107. $ShortType = 'int'; // or 'banner'
  108. $short_url = file_get_contents('http://adf.ly/api.php?key=' . $APIKey . '&uid=' . $UserID . '&advert_type=' . $ShortType . '&url=' . $ToConvert);
  109. return $short_url;
  110. }
  111.  
  112. // Kwn.me shortener
  113. function shortKwnme($ToConvert) {
  114. $short_url = file_get_contents('http://kwn.me/t.php?process=1&url=' . $ToConvert);
  115. return $short_url;
  116. }
  117.  
  118. // Brif.us shortener
  119. function shortBrifus($ToConvert) {
  120. $short_url = file_get_contents('http://brif.us/api.php?action=shorturl&format=simple&url=' . $ToConvert);
  121. return $short_url;
  122. }
  123. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.