davidjmorin

google url shortener

Feb 29th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4.  
  5. <style type="text/css">
  6. #shorten_container{margin-top: 20px;background: none repeat scroll 0 0 #66b5e5; padding: 0.5em 1em; vertical-align: middle; white-space: nowrap;}
  7. #longUrl { border: 1px solid #666666; height: 1.33em; margin-right: 0.2em; padding: 0; width: 19em;font-size: 25px}
  8. input[type=submit]{font-size: 25px}
  9. table {font-size: 20px;padding: 10px}
  10. table td{border: 1px solid #999;padding: 5px}
  11. label{font-weight: bold}
  12. </style>
  13.  
  14. </head>
  15. <body>
  16. <div id="doc2" class="yui-t7">
  17. <div id="hd" role="banner"><h1>My Droid Central URL shortener</h1></div>
  18. <div id="bd" role="main">
  19. <p id="intro">Shorten your URL here!.</p>
  20. <div class="yui-g">
  21.  
  22. <?php
  23. //grab your Shortner KEY
  24. define('GOOGLE_API_KEY',"AIzaSyBeFQk8nh8PHErHAl9jn9Au77B3j8SKIm0");
  25. //define endpoint Shortner
  26. define('GOOGLE_ENDPOINT',"https://www.googleapis.com/urlshortener/v1/url");
  27.  
  28. //function for shortner long URL
  29. function shortenUrl($longUrl) {
  30.  
  31. $url = sprintf("%s?key=%s",GOOGLE_ENDPOINT,GOOGLE_API_KEY);
  32. $ch = curl_init();
  33. curl_setopt($ch, CURLOPT_URL, $url);
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  37. curl_setopt($ch, CURLOPT_HEADER, 0);
  38. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
  39. curl_setopt($ch, CURLOPT_POST, 1);
  40. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("longUrl"=>$longUrl)));
  41. $data = curl_exec($ch);
  42. curl_close($ch);
  43.  
  44. return json_decode($data,true);
  45. }
  46.  
  47. //function for reverse URL
  48. function expandUrl($shortUrl) {
  49.  
  50. $url = sprintf("%s?key=%s&shortUrl=%s",GOOGLE_ENDPOINT,GOOGLE_API_KEY,$shortUrl);
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL, $url);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  54. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  56. $data = curl_exec($ch);
  57. curl_close($ch);
  58.  
  59. return json_decode($data,true);
  60. }
  61.  
  62.  
  63.  
  64. echo"<div id='shorten_container'>";
  65. echo"<form action=".$_SERVER['PHP_SELF']." method='get'>";
  66. echo"<p><label for='longUrl'>Paste your long url here:</label></p>";
  67. echo"<p><input type='text' id='longUrl' name='longUrl'/><input type='submit' name='shorten' value='Submit'></p>";
  68. echo"</form>";
  69. echo"</div>";
  70. echo"<div id='result'>";
  71. if(isset($_GET['longUrl']) && isset($_GET['shorten'])) {
  72. $response = shortenUrl($_GET['longUrl']);
  73. $short= $response['id'];
  74. $long = $response['longUrl'];
  75. echo"<table>";
  76. echo"<tr><td>$short</td></tr>";
  77. echo"</table>";
  78. }
  79. echo"</div>";
  80.  
  81. ?>
  82.  
  83.  
  84.  
  85. </div>
  86. </div>
  87. </div>
  88. </body>
  89. </html>
Add Comment
Please, Sign In to add comment