Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <style type="text/css">
- #shorten_container{margin-top: 20px;background: none repeat scroll 0 0 #66b5e5; padding: 0.5em 1em; vertical-align: middle; white-space: nowrap;}
- #longUrl { border: 1px solid #666666; height: 1.33em; margin-right: 0.2em; padding: 0; width: 19em;font-size: 25px}
- input[type=submit]{font-size: 25px}
- table {font-size: 20px;padding: 10px}
- table td{border: 1px solid #999;padding: 5px}
- label{font-weight: bold}
- </style>
- </head>
- <body>
- <div id="doc2" class="yui-t7">
- <div id="hd" role="banner"><h1>My Droid Central URL shortener</h1></div>
- <div id="bd" role="main">
- <p id="intro">Shorten your URL here!.</p>
- <div class="yui-g">
- <?php
- //grab your Shortner KEY
- define('GOOGLE_API_KEY',"AIzaSyBeFQk8nh8PHErHAl9jn9Au77B3j8SKIm0");
- //define endpoint Shortner
- define('GOOGLE_ENDPOINT',"https://www.googleapis.com/urlshortener/v1/url");
- //function for shortner long URL
- function shortenUrl($longUrl) {
- $url = sprintf("%s?key=%s",GOOGLE_ENDPOINT,GOOGLE_API_KEY);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("longUrl"=>$longUrl)));
- $data = curl_exec($ch);
- curl_close($ch);
- return json_decode($data,true);
- }
- //function for reverse URL
- function expandUrl($shortUrl) {
- $url = sprintf("%s?key=%s&shortUrl=%s",GOOGLE_ENDPOINT,GOOGLE_API_KEY,$shortUrl);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $data = curl_exec($ch);
- curl_close($ch);
- return json_decode($data,true);
- }
- echo"<div id='shorten_container'>";
- echo"<form action=".$_SERVER['PHP_SELF']." method='get'>";
- echo"<p><label for='longUrl'>Paste your long url here:</label></p>";
- echo"<p><input type='text' id='longUrl' name='longUrl'/><input type='submit' name='shorten' value='Submit'></p>";
- echo"</form>";
- echo"</div>";
- echo"<div id='result'>";
- if(isset($_GET['longUrl']) && isset($_GET['shorten'])) {
- $response = shortenUrl($_GET['longUrl']);
- $short= $response['id'];
- $long = $response['longUrl'];
- echo"<table>";
- echo"<tr><td>$short</td></tr>";
- echo"</table>";
- }
- echo"</div>";
- ?>
- </div>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment