Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial code commit

  • Loading branch information...
commit 4349eb6d6d238a4e4eff5d5a9aef24cda583a3ba 0 parents
@tommcfarlin authored
Showing with 168 additions and 0 deletions.
  1. +7 −0 README.txt
  2. +30 −0 index.html
  3. +42 −0 javascript/urldemo.js
  4. +89 −0 php/curl.php
7 README.txt
@@ -0,0 +1,7 @@
+Google URL Shortener API Demo
+A simple demonstration for how to get started using the Google API demo. Requires a generic LAMP installation.
+
+CONTACT ME
+
+Website: http://tommcfarlin.com
+Email: tom@tommcfarlin.com
30 index.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
+ <script type="text/javascript" src="javascript/urldemo.js"></script>
+ <title>Google URL Shortener Demo</title>
+ </head>
+ <body>
+ <div id="container">
+ <h1>Google URL Shortener Demo</h1>
+ <div id="wrapper">
+ <h2>Shorten a Long URL</h2>
+ <input type="text" id="long-url" name="long-url" value="" />
+ <a href="javascript:;" id="shorten-url">
+ Shorten URL
+ </a>
+ <span id="shortened-url"></span>
+ </div>
+ </div>
+ <div class="wrapper">
+ <h2>Lengthen a URL</h2>
+ <input type="text" id="short-url" name="short-url" value="" />
+ <a href="javascript:;" id="expand-url">
+ Expand URL
+ </a>
+ <span id="lengthened-url"></span>
+ </div>
+ </body>
+</html>
42 javascript/urldemo.js
@@ -0,0 +1,42 @@
+/**
+ * Google URL Shortener API Example
+ * Tom McFarlin / tom@tommcfarlin.com / January 15, 2010
+ *
+ * Simple demonstration of how to use the the Google URL API shortener.
+ */
+
+$(function() {
+
+ $('#shorten-url').click(function(evt) {
+ evt.preventDefault();
+ if($('#long-url').length === 0 || $('#long-url').val() === '') {
+ alert('You need to enter a URL to shorten.');
+ } else {
+ $.ajax({
+ type: 'POST',
+ url: 'php/curl.php',
+ data: 'longUrl=' + $('#long-url').val(),
+ success: function(data) {
+ $('#shortened-url').html(data);
+ }
+ });
+ } // end if/else
+ });
+
+ $('#expand-url').click(function(evt) {
+ evt.preventDefault();
+ if($('#short-url').length === 0 || $('#short-url').val() === '') {
+ alert('You need to enter a URL to expand.');
+ } else {
+ $.ajax({
+ type: 'GET',
+ url: 'php/curl.php',
+ data: 'shortUrl=' + $('#short-url').val(),
+ success: function(data) {
+ $('#lengthened-url').html(data);
+ }
+ });
+ } // end if/else
+ });
+
+});
89 php/curl.php
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * Google URL Shortener API Example
+ * Tom McFarlin / tom@tommcfarlin.com / January 15, 2010
+ *
+ * Simple demonstration of how to use the the Google URL API shortener.
+ */
+
+ /*------------------------------------------*/
+ /* Constants
+ /*------------------------------------------*/
+
+ define('GOOGLE_URL', 'https://www.googleapis.com/urlshortener/v1/url');
+ define('GOOGLE_API_KEY', 'YOUR_API_KEY_HERE');
+
+ /*------------------------------------------*/
+ /* Request Data
+ /*------------------------------------------*/
+
+ $longUrl = $_POST['longUrl'];
+ $shortUrl = $_GET['shortUrl'];
+
+ if(isset($longUrl)) {
+ echo shorten($longUrl);
+ } elseif(isset($shortUrl)) {
+ echo expand($shortUrl);
+ } else {
+ echo 'You must enter a URL.';
+ } // end if/else
+
+ /*------------------------------------------*/
+ /* API Functions
+ /*------------------------------------------*/
+
+ /**
+ * Shortens the incoming URL using the Google URL shortener API
+ * and returns the shortened version.
+ *
+ * @long_url The URL to shorten.
+ */
+ function shorten($long_url) {
+
+ $ch = curl_init(GOOGLE_URL . '?key=' . GOOGLE_API_KEY);
+
+ curl_setopt_array(
+ $ch,
+ array(
+ CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
+ CURLOPT_RETURNTRANSFER => 1,
+ CURLOPT_TIMEOUT => 5,
+ CURLOPT_CONNECTTIMEOUT => 0,
+ CURLOPT_POST => 1,
+ CURLOPT_SSL_VERIFYHOST => 0,
+ CURLOPT_SSL_VERIFYPEER => 0,
+ CURLOPT_POSTFIELDS => '{"longUrl": "' . $long_url . '"}'
+ )
+ );
+
+ $json_response = json_decode(curl_exec($ch), true);
+ return $json_response['id'] ? $json_response['id'] : $long_url;
+
+ } // end shorten
+
+ /**
+ * Expands the short URL using the Google shorten API.
+ *
+ * @short_url The URL to expand.
+ */
+ function expand($short_url) {
+
+ $ch = curl_init(GOOGLE_URL . '?key=' . GOOGLE_API_KEY . '&shortUrl=' . $short_url);
+
+ curl_setopt_array(
+ $ch,
+ array(
+ CURLOPT_RETURNTRANSFER => 1,
+ CURLOPT_TIMEOUT => 5,
+ CURLOPT_CONNECTTIMEOUT => 0,
+ CURLOPT_SSL_VERIFYHOST => 0,
+ CURLOPT_SSL_VERIFYPEER => 0
+ )
+ );
+
+ $json_response = json_decode(curl_exec($ch), true);
+ return $json_response['longUrl'] ? $json_response['longUrl'] : $short_url;
+ } // end expand
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.