Cara Membuat URL Shortener dengan API Bit.ly

Langsung aja Copy script ini ke notepad

Demo : http://siyt.biz/url

  1. <html>
  2. <head>
  3. <title>Make Your Own URL Shortener with Bit.ly API</title>
  4. <style type=”text/css”>
  5. html { font-family:arial;}
  6. p {font-size:12px;}
  7. input { font-size:20px;}
  8. a, span.url { color:#990000; }
  9. </style>
  10. <script type=”text/javascript”>
  11. function cek(f) {
  12. if (f.longurl.value == ”) {
  13. alert (“Enter your long URL”);
  14. return false;
  15. } else {
  16. return true;
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <h1>URL Shortener with Bit.ly API</h1>
  23. <div style=”border:1px solid #000000;padding:5px”>
  24. <form action=”” method=”post” onsubmit=”return cek(this)”>
  25. <p>Enter your long url and click “Shorten” button.</p>
  26. <input type=”text” name=”longurl” value=”” size=”40″/>
  27. <input type=”submit” name=”shorten” value=”Shorten”/>
  28. </form>
  29. <?php
  30. if (isset($_POST['shorten' ])) {
  31. $longurl = $_POST['longurl' ];
  32. $login = “username”; //username login bit.ly
  33. $appkey = “api-key”; //API key
  34. $shorturl = make_bitly_url ($longurl, $login, $appkey, ‘json’ );
  35. echo “<p><strong>Your long URL is</strong> <span class=’url’>$longurl </span></p>”;
  36. echo “<p><strong>Your short URL is</strong> <span class=’url’>$shorturl </span></p>”;
  37. echo “<p><strong>Try your shorten URL:</strong> <a href=’$shorturl‘ target=’_blank’>$shorturl</a></p>”;
  38. }
  39. ?>
  40. </div>
  41. </body>
  42. </html>
  43.  
  44. <?php
  45.  
  46. /* make a URL small with bit.ly */
  47. function make_bitly_url($url, $login, $appkey, $format = ‘xml’ ,$version = ‘2.0.1’ )
  48. {
  49. //create the URL
  50. $bitly = ‘http://api.bit.ly/shorten?version=’ .$version.‘&longUrl=’ .urlencode($url). ‘&login=’ .$login.‘&apiKey=’ .$appkey.‘&format=’ .$format;
  51.  
  52. //get the url
  53. //could also use cURL here
  54. $response = file_get_contents($bitly);
  55.  
  56. //parse depending on desired format
  57. if(strtolower($format) == ‘json’ ) {
  58. $json = @json_decode($response,true);
  59. return $json['results' ][$url]['shortUrl' ];
  60. } else { //xml
  61. $xml = simplexml_load_string($response);
  62. return ‘http://bit.ly/’ .$xml->results->nodeKeyVal->hash;
  63. }
  64. }
  65. ?>

 

  1. <html>
  2. <head>
  3. <title>Make Your Own URL Shortener with Bit.ly API</title>
  4. <style type=”text/css”>
  5. html { font-family:arial;}
  6. p {font-size:12px;}
  7. input { font-size:20px;}
  8. a, span.url { color:#990000; }
  9. </style>
  10. <script type=”text/javascript”>
  11. function cek(f) {
  12. if (f.longurl.value == ”) {
  13. alert (“Enter your long URL”);
  14. return false;
  15. } else {
  16. return true;
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <h1>URL Shortener with Bit.ly API</h1>
  23. <div style=”border:1px solid #000000;padding:5px”>
  24. <form action=”” method=”post” onsubmit=”return cek(this)”>
  25. <p>Enter your long url and click “Shorten” button.</p>
  26. <input type=”text” name=”longurl” value=”” size=”40″/>
  27. <input type=”submit” name=”shorten” value=”Shorten”/>
  28. </form>
  29. <?php
  30. if (isset($_POST['shorten' ])) {
  31. $longurl = $_POST['longurl' ];
  32. $login = “username”; //username login bit.ly
  33. $appkey = “api-key”; //API key
  34. $shorturl = make_bitly_url ($longurl, $login, $appkey, ‘json’ );
  35. echo “<p><strong>Your long URL is</strong> <span class=’url’>$longurl </span></p>”;
  36. echo “<p><strong>Your short URL is</strong> <span class=’url’>$shorturl </span></p>”;
  37. echo “<p><strong>Try your shorten URL:</strong> <a href=’$shorturl‘ target=’_blank’>$shorturl</a></p>”;
  38. }
  39. ?>
  40. </div>
  41. </body>
  42. </html>
  43.  
  44. <?php
  45.  
  46. /* make a URL small with bit.ly */
  47. function make_bitly_url($url, $login, $appkey, $format = ‘xml’ ,$version = ‘2.0.1’ )
  48. {
  49. //create the URL
  50. $bitly = ‘http://api.bit.ly/shorten?version=’ .$version.‘&longUrl=’ .urlencode($url). ‘&login=’ .$login.‘&apiKey=’ .$appkey.‘&format=’ .$format;
  51.  
  52. //get the url
  53. //could also use cURL here
  54. $response = file_get_contents($bitly);
  55.  
  56. //parse depending on desired format
  57. if(strtolower($format) == ‘json’ ) {
  58. $json = @json_decode($response,true);
  59. return $json['results' ][$url]['shortUrl' ];
  60. } else { //xml
  61. $xml = simplexml_load_string($response);
  62. return ‘http://bit.ly/’ .$xml->results->nodeKeyVal->hash;
  63. }
  64. }
  65. ?>

Leave a Reply

Your email address will not be published. Required fields are marked *