├── README.mkd ├── config └── google_url_api.php ├── controller └── google_url.php └── libraries └── Google_url_api.php /README.mkd: -------------------------------------------------------------------------------- 1 | CodeIgniter Google URL Shortener API Library 2 | =============== 3 | 4 | Welcome to the home of the Google URL Shortener API Library, a project that will help you to shorten, expand, analytics long URL. 5 | 6 | What is the Google URL Shortener API? 7 | =============== 8 | The Google URL Shortener at goo.gl is a service that takes long URLs and squeezes them into fewer characters to make a link that is easier to share, tweet, or email to friends. 9 | The Google URL Shortener API allows you to develop applications that interface with this service. You can use simple HTTP methods to create, inspect, and manage goo.gl short URLs from your desktop, mobile, or web application. 10 | 11 | ref : http://code.google.com/intl/zh-TW/apis/urlshortener/ 12 | 13 | Spark Install 14 | =============== 15 | 16 | Please referrer from http://getsparks.org/packages/google-url-shortener/versions/HEAD/show 17 | 18 | INSTALLATION 19 | =============== 20 | 21 | Download all file from this site. 22 | 23 | $ https://appleboy@github.com/appleboy/CodeIgniter-Google-URL-Shortener-API.git 24 | $ git clone git://github.com/appleboy/CodeIgniter-Google-URL-Shortener-API.git 25 | 26 | Move the files to their corresponding places within your codeigniter application directory. 27 | 28 | $ copy config/google_url_api.php your_application/config/ 29 | $ copy controller/google_url.php your_application/controller/ 30 | $ copy libraries/Google_url_api.php your_application/libraries/ 31 | 32 | Open config/google_url_api.php file, and configure your shorten api key 33 | 34 | $config['google_api_key'] = "xxxxx"; 35 | 36 | Test your controller file: google_url.php 37 | 38 | http://localhost/google_url/ 39 | 40 | If you're having problems, then enable debugging in your controller code: 41 | 42 | $this->google_url_api->enable_debug(TRUE); 43 | 44 | COPYRIGHT AND LICENCE 45 | =============== 46 | 47 | Copyright (C) 2011 Bo-Yi Wu ( appleboy AT gmail.com ) 48 | -------------------------------------------------------------------------------- /config/google_url_api.php: -------------------------------------------------------------------------------- 1 | load->library('google_url_api'); 9 | } 10 | 11 | function index() 12 | { 13 | 14 | $url = 'http://www.google.com'; 15 | /* if you want switch debug mode, please replace FALSE with TRUE*/ 16 | $this->google_url_api->enable_debug(FALSE); 17 | 18 | /** 19 | * shorten example 20 | */ 21 | 22 | echo '
'; 55 | var_dump($data); 56 | echo ''; 57 | } 58 | 59 | private function _print($data) 60 | { 61 | echo ''; 62 | print_r($data); 63 | echo ''; 64 | } 65 | } 66 | 67 | /* End of file google_url.php */ 68 | /* Location: ./application/controller/google_url.php */ 69 | 70 | -------------------------------------------------------------------------------- /libraries/Google_url_api.php: -------------------------------------------------------------------------------- 1 | 9 | * @created 2011-02-17 10 | * @reference http://code.google.com/intl/zh-TW/apis/urlshortener/ 11 | * 12 | */ 13 | 14 | class Google_url_api { 15 | 16 | private $_api_url; 17 | private $_api_key; 18 | private $_enable_debug = FALSE; 19 | private $http_status; 20 | 21 | function __construct() 22 | { 23 | $this->_obj =& get_instance(); 24 | $this->_obj->load->config('google_url_api'); 25 | $this->_api_url = $this->_obj->config->item("google_api_url"); 26 | $this->_api_key = $this->_obj->config->item("google_api_key"); 27 | $this->_api_url = $this->_api_url . '?key=' . $this->_api_key; 28 | } 29 | 30 | /** 31 | * Shorten a long URL 32 | * 33 | * @param $url 34 | * @return JSON object 35 | */ 36 | public function shorten($url) 37 | { 38 | $response = $this->send($url, 'shorten'); 39 | ($this->_enable_debug) AND $this->_dump($response); 40 | return $response; 41 | } 42 | 43 | /** 44 | * Expand a short URL 45 | * 46 | * @param $url 47 | * @return JSON object 48 | */ 49 | public function expand($url) 50 | { 51 | $response = $this->send($url, 'expand'); 52 | ($this->_enable_debug) AND $this->_dump($response); 53 | return $response; 54 | } 55 | 56 | /** 57 | * Look up a short URL's analytics 58 | * 59 | * @param $url 60 | * @param $projection 61 | * FULL - returns the creation timestamp and all available analytics 62 | * ANALYTICS_CLICKS - returns only click counts 63 | * ANALYTICS_TOP_STRINGS - returns only top string counts (e.g. referrers, countries, etc) 64 | */ 65 | public function analytics($url, $projection = NULL) 66 | { 67 | $projection = (isset($projection)) ? $projection : 'FULL'; 68 | $data = array("projection" => $projection); 69 | $response = $this->send($url, 'analytics', $data); 70 | ($this->_enable_debug) AND $this->_dump($response); 71 | return $response; 72 | } 73 | 74 | /** 75 | * function send 76 | * Connect to Google URL API 77 | * 78 | * @param $url 79 | * @param $action 80 | * @param $data 81 | * 82 | * return JSON object 83 | */ 84 | function send($url, $action = NULL, $data = NULL) 85 | { 86 | 87 | $action = (isset($action)) ? $action : 'shorten'; 88 | $data = (isset($data)) ? $data : array(); 89 | 90 | $ch = curl_init(); 91 | 92 | if($action == 'shorten') 93 | { 94 | /* POST Url */ 95 | $data['longUrl'] = $url; 96 | curl_setopt($ch, CURLOPT_URL, $this->_api_url); 97 | curl_setopt($ch, CURLOPT_POST, 1); 98 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 99 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 100 | } 101 | else if($action == 'expand' OR $action == 'analytics') 102 | { 103 | /* Get Url*/ 104 | $data['shortUrl'] = $url; 105 | $api_url = $this->_api_url . '&' . http_build_query($data); 106 | curl_setopt($ch, CURLOPT_URL, $api_url); 107 | if($this->_enable_debug) echo $api_url . "
"; 108 | } 109 | 110 | /* WARNING: this would prevent curl from detecting a 'man in the middle' attack */ 111 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 112 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 113 | 114 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 115 | 116 | $result = curl_exec($ch); 117 | 118 | /* show error message */ 119 | if($this->_enable_debug) 120 | { 121 | if(!curl_errno($ch)) 122 | { 123 | $info = curl_getinfo($ch); 124 | echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'] . "
"; 125 | } 126 | else 127 | { 128 | echo 'Curl error: ' . curl_error($ch) . "
"; 129 | } 130 | } 131 | 132 | $this->http_response = $result; 133 | $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 134 | 135 | curl_close($ch); 136 | return json_decode($result); 137 | } 138 | 139 | /** 140 | * function get_http_status 141 | * Get HTTP Status Code 142 | * 143 | * @return int 144 | */ 145 | public function get_http_status() 146 | { 147 | return (int) $this->http_status; 148 | } 149 | 150 | /** 151 | * protected function _dump 152 | * dump array or object data 153 | * 154 | * @return NULL 155 | */ 156 | protected function _dump($data) 157 | { 158 | echo ''; 159 | var_dump($data); 160 | echo ''; 161 | } 162 | 163 | public function enable_debug($debug = true) 164 | { 165 | $this->_enable_debug = (bool) $debug; 166 | } 167 | } 168 | 169 | /* End of file Google_url_api.php */ 170 | /* Location: ./application/libraries/Google_url_api.php */ 171 | 172 | --------------------------------------------------------------------------------