Posted By

stamba on 11/14/10


Tagged

url text shorteners


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

proto


Find URLs In a Text and Shorten Them


 / Published in: PHP
 

URL: http://forr.st/~Cyl

This script parses the entered text and it gets all URLs from the text. It will then shorten those URLs and replace the long URLs with shortened ones in the text. If you need only the Shortener Services script, take a look at this snippet http://snipplr.com/view/44115/shortener-services/

  1. <?php
  2. /*
  3.   * To use Bit.ly service you need to enter your Username and API Key below
  4.   * To use Adf.ly service you need to enter your User ID and API Key below
  5.   */
  6.  
  7. // Get the args
  8. $text = $_POST['text'];
  9. $shortener = $_GET['shortener'];
  10.  
  11. // Explode the submited text
  12. $pieces = explode(" ", $text);
  13.  
  14. // For each element in array check if it is a link, shorten and replace it in passed text
  15. foreach ($pieces as $piece) {
  16. if (preg_match("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", $piece)) {
  17. $newsmallurl = call_shortener($shortener, $piece);
  18. $text = str_replace($piece, $newsmallurl, $text);
  19. }
  20. }
  21.  
  22. // Print final text (with shortened URLs) and send it to Twitter as a status
  23. echo $text . "<br /><br />";
  24. $textenc = urlencode($text);
  25. echo "<a href='http://twitter.com/home?status=$textenc'>Tweet this!</a>";
  26.  
  27. // Choose the correct function based on the passed argument
  28. function call_shortener($shortener, $passedurl) {
  29. // Determine which function to call
  30. if ($shortener == 'tinyurl') {
  31. $shorturl = shortTinyURL($passedurl);
  32. } elseif ($shortener == 'bitly') {
  33. $shorturl = shortBitly($passedurl);
  34. } elseif ($shortener == 'supr') {
  35. $shorturl = shortSupr($passedurl);
  36. } elseif ($shortener == 'isgd') {
  37. $shorturl = shortIsgd($passedurl);
  38. } elseif ($shortener == 'l4uin') {
  39. $shorturl = shortL4uin($passedurl);
  40. } elseif ($shortener == 'toly') {
  41. $shorturl = shortToly($passedurl);
  42. } elseif ($shortener == 'adfly') {
  43. $shorturl = shortAdfly($passedurl);
  44. } elseif ($shortener == 'kwnme') {
  45. $shorturl = shortKwnme($passedurl);
  46. }
  47.  
  48. // Return the shortened URL
  49. return $shorturl;
  50. }
  51.  
  52. // TinyURL shortener
  53. function shortTinyURL($ToConvert) {
  54. $short_url = file_get_contents('http://tinyurl.com/api-create.php?url=' . $ToConvert);
  55. return $short_url;
  56. }
  57.  
  58. // Bit.ly shortener
  59. function shortBitly($ToConvert) {
  60. $bitlylogin = 'YOUR_USERNAME';
  61. $bitlyapikey = 'YOUR_API_KEY';
  62. $bitlyurl = file_get_contents('http://api.bit.ly/shorten?version=2.0.1&longUrl=' . $ToConvert . '&login=' . $bitlylogin . '&apiKey=' . $bitlyapikey);
  63. $bitlycontent = json_decode($bitlyurl,true);
  64. $bitlyerror = $bitlycontent['errorCode'];
  65. $short_url = $bitlycontent['results'][$ToConvert]['shortUrl'];
  66. return $short_url;
  67. }
  68.  
  69. // Su.pr shortener
  70. function shortSupr($ToConvert) {
  71. $short_url = file_get_contents('http://su.pr/api?url=' . $ToConvert);
  72. return $short_url;
  73. }
  74.  
  75. // Is.gd shortener
  76. function shortIsgd($ToConvert) {
  77. $short_url = file_get_contents('http://www.is.gd/api.php?longurl=' . $ToConvert);
  78. return $short_url;
  79. }
  80.  
  81. // L4u.in shortener
  82. function shortL4uin($ToConvert) {
  83. $short_url = file_get_contents('http://www.l4u.in/?module=ShortURL&file=Add&mode=API&url=' . $ToConvert);
  84. return $short_url;
  85. }
  86.  
  87. // To.ly shortener
  88. function shortToly($ToConvert) {
  89. $ch = curl_init();
  90. curl_setopt($ch, CURLOPT_URL, "http://to.ly/api.php?longurl=".urlencode($ToConvert));
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  92. curl_setopt($ch, CURLOPT_HEADER, 0);
  93. $shorturl = curl_exec ($ch);
  94. curl_close ($ch);
  95. return $short_url;
  96. }
  97.  
  98. // Adf.ly shortener
  99. function shortAdfly($ToConvert) {
  100. $APIKey = 'YOUR_API_KEY';
  101. $UserID = 'YOUR_USER_ID';
  102. $ShortType = 'int'; // or 'banner'
  103. $short_url = file_get_contents('http://adf.ly/api.php?key=' . $APIKey . '&uid=' . $UserID . '&advert_type=' . $ShortType . '&url=' . $ToConvert);
  104. return $short_url;
  105. }
  106.  
  107. // Kwn.me shortener
  108. function shortKwnme($ToConvert) {
  109. $short_url = file_get_contents('http://kwn.me/t.php?process=1&url=' . $ToConvert);
  110. return $short_url;
  111. }
  112. ?>

Report this snippet  

You need to login to post a comment.