├── README └── application ├── config └── bitly.php ├── controllers └── bitly_test.php └── libraries └── bitly.php /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elliothaughin/codeigniter-bitly/02651b9d44a2d5ea73547fdc8a11258c84c5f6c0/README -------------------------------------------------------------------------------- /application/config/bitly.php: -------------------------------------------------------------------------------- 1 | load->add_package_path('/Users/elliot/sites/github/codeigniter-bitly/application/'); 13 | $this->load->library('bitly'); 14 | $this->bitly->enable_debug(TRUE); 15 | 16 | $longUrl = 'http://www.haughin.com/code/'; 17 | 18 | $request_shorten = $this->bitly->call('shorten', array('longUrl' => $longUrl)); 19 | $this->_dump($request_shorten->data); 20 | 21 | $request_expand = $this->bitly->call('expand', array('shortUrl' => $request_shorten->data->url)); 22 | $this->_dump($request_expand->data); 23 | 24 | $request_info = $this->bitly->call('info', array('shortUrl' => $request_shorten->data->url)); 25 | $this->_dump($request_info->data); 26 | 27 | $request_global_info = $this->bitly->call('info', array('hash' => $request_shorten->data->global_hash)); 28 | $this->_dump($request_global_info->data); 29 | 30 | $request_clicks = $this->bitly->call('clicks', array('shortUrl' => $request_shorten->data->url)); 31 | $this->_dump($request_clicks->data); 32 | 33 | $request_global_clicks = $this->bitly->call('clicks', array('hash' => $request_shorten->data->global_hash)); 34 | $this->_dump($request_global_clicks->data); 35 | 36 | $request_referrers = $this->bitly->call('referrers', array('shortUrl' => $request_shorten->data->url)); 37 | $this->_dump($request_referrers->data); 38 | 39 | $request_global_referrers = $this->bitly->call('referrers', array('hash' => $request_shorten->data->global_hash)); 40 | $this->_dump($request_global_referrers->data); 41 | 42 | $request_countries = $this->bitly->call('countries', array('shortUrl' => $request_shorten->data->url)); 43 | $this->_dump($request_countries->data); 44 | 45 | $request_global_countries = $this->bitly->call('countries', array('hash' => $request_shorten->data->global_hash)); 46 | $this->_dump($request_global_countries->data); 47 | 48 | $request_clicks_min = $this->bitly->call('clicks_by_minute', array('shortUrl' => $request_shorten->data->url)); 49 | $this->_dump($request_clicks_min->data); 50 | 51 | $request_global_clicks_min = $this->bitly->call('clicks_by_minute', array('hash' => $request_shorten->data->global_hash)); 52 | $this->_dump($request_global_clicks_min->data); 53 | 54 | $request_clicks_day = $this->bitly->call('clicks_by_day', array('shortUrl' => $request_shorten->data->url)); 55 | $this->_dump($request_clicks_day->data); 56 | 57 | $request_global_clicks_day = $this->bitly->call('clicks_by_day', array('hash' => $request_shorten->data->global_hash)); 58 | $this->_dump($request_global_clicks_day->data); 59 | } 60 | 61 | private function _dump($data) 62 | { 63 | echo '
'; 64 | var_dump($data); 65 | echo ''; 66 | } 67 | } -------------------------------------------------------------------------------- /application/libraries/bitly.php: -------------------------------------------------------------------------------- 1 | _obj =& get_instance(); 13 | $this->_obj->load->config('bitly'); 14 | 15 | $this->_api_url = $this->_obj->config->item('bitly_api_url'); 16 | $this->_api_user = $this->_obj->config->item('bitly_api_user'); 17 | $this->_api_key = $this->_obj->config->item('bitly_api_key'); 18 | 19 | $this->connection = new bitlyConnection(); 20 | } 21 | 22 | public function call($uri, $data = array()) 23 | { 24 | $response = FALSE; 25 | 26 | try 27 | { 28 | $response = $this->connection->get($this->append_token($this->_api_url.$uri), $data); 29 | } 30 | catch (bitlyException $e) 31 | { 32 | $this->_errors[] = $e; 33 | 34 | if ( $this->_enable_debug ) 35 | { 36 | echo $e; 37 | } 38 | } 39 | 40 | return $response; 41 | } 42 | 43 | public function errors() 44 | { 45 | return $this->_errors; 46 | } 47 | 48 | public function last_error() 49 | { 50 | if ( count($this->_errors) == 0 ) return NULL; 51 | 52 | return $this->_errors[count($this->_errors) - 1]; 53 | } 54 | 55 | public function append_token($url) 56 | { 57 | return $url.'?format=json&login='.$this->_api_user.'&apiKey='.$this->_api_key.'&'; 58 | } 59 | 60 | 61 | public function enable_debug($debug = TRUE) 62 | { 63 | $this->_enable_debug = (bool) $debug; 64 | } 65 | } 66 | 67 | class bitlyConnection { 68 | 69 | // Allow multi-threading. 70 | 71 | private $_mch = NULL; 72 | private $_properties = array(); 73 | 74 | function __construct() 75 | { 76 | $this->_mch = curl_multi_init(); 77 | 78 | $this->_properties = array( 79 | 'code' => CURLINFO_HTTP_CODE, 80 | 'time' => CURLINFO_TOTAL_TIME, 81 | 'length' => CURLINFO_CONTENT_LENGTH_DOWNLOAD, 82 | 'type' => CURLINFO_CONTENT_TYPE 83 | ); 84 | } 85 | 86 | private function _initConnection($url) 87 | { 88 | $this->_ch = curl_init($url); 89 | curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE); 90 | } 91 | 92 | public function get($url, $params = array()) 93 | { 94 | if ( count($params) > 0 ) 95 | { 96 | foreach( $params as $k => $v ) 97 | { 98 | $url .= "{$k}={$v}&"; 99 | } 100 | 101 | $url = substr($url, 0, -1); 102 | } 103 | 104 | $this->_initConnection($url); 105 | $response = $this->_addCurl($url, $params); 106 | 107 | return $response; 108 | } 109 | 110 | private function _addCurl($url, $params = array()) 111 | { 112 | $ch = $this->_ch; 113 | 114 | $key = (string) $ch; 115 | $this->_requests[$key] = $ch; 116 | 117 | $response = curl_multi_add_handle($this->_mch, $ch); 118 | 119 | if ( $response === CURLM_OK || $response === CURLM_CALL_MULTI_PERFORM ) 120 | { 121 | do { 122 | $mch = curl_multi_exec($this->_mch, $active); 123 | } while ( $mch === CURLM_CALL_MULTI_PERFORM ); 124 | 125 | return $this->_getResponse($key); 126 | } 127 | else 128 | { 129 | return $response; 130 | } 131 | } 132 | 133 | private function _getResponse($key = NULL) 134 | { 135 | if ( $key == NULL ) return FALSE; 136 | 137 | if ( isset($this->_responses[$key]) ) 138 | { 139 | return $this->_responses[$key]; 140 | } 141 | 142 | $running = NULL; 143 | 144 | do 145 | { 146 | $response = curl_multi_exec($this->_mch, $running_curl); 147 | 148 | if ( $running !== NULL && $running_curl != $running ) 149 | { 150 | $this->_setResponse($key); 151 | 152 | if ( isset($this->_responses[$key]) ) 153 | { 154 | $response = new bitlyResponse( (object) $this->_responses[$key] ); 155 | 156 | if ( $response->__resp->code !== 200 ) 157 | { 158 | $error = $response->__resp->code.' | Request Failed'; 159 | 160 | if ( isset($response->__resp->data->error->type) ) 161 | { 162 | $error .= ' - '.$response->__resp->data->error->type.' - '.$response->__resp->data->error->message; 163 | } 164 | 165 | throw new bitlyException($error); 166 | } 167 | 168 | return $response; 169 | } 170 | } 171 | 172 | $running = $running_curl; 173 | 174 | } while ( $running_curl > 0); 175 | 176 | } 177 | 178 | private function _setResponse($key) 179 | { 180 | while( $done = curl_multi_info_read($this->_mch) ) 181 | { 182 | $key = (string) $done['handle']; 183 | $this->_responses[$key]['data'] = curl_multi_getcontent($done['handle']); 184 | 185 | foreach ( $this->_properties as $curl_key => $value ) 186 | { 187 | $this->_responses[$key][$curl_key] = curl_getinfo($done['handle'], $value); 188 | 189 | curl_multi_remove_handle($this->_mch, $done['handle']); 190 | } 191 | } 192 | } 193 | } 194 | 195 | class bitlyResponse { 196 | 197 | private $__construct; 198 | 199 | public function __construct($resp) 200 | { 201 | $this->__resp = $resp; 202 | 203 | $data = json_decode($this->__resp->data); 204 | 205 | if ( $data !== NULL ) 206 | { 207 | $this->__resp->data = $data; 208 | } 209 | } 210 | 211 | public function __get($name) 212 | { 213 | if ($this->__resp->code < 200 || $this->__resp->code > 299) return FALSE; 214 | 215 | $result = array(); 216 | 217 | if ( is_string($this->__resp->data ) ) 218 | { 219 | parse_str($this->__resp->data, $result); 220 | $this->__resp->data = (object) $result; 221 | } 222 | 223 | if ( $name === '_result') 224 | { 225 | return $this->__resp->data; 226 | } 227 | 228 | return $this->__resp->data->$name; 229 | } 230 | } 231 | 232 | class bitlyException extends Exception { 233 | 234 | function __construct($string) 235 | { 236 | parent::__construct($string); 237 | } 238 | 239 | public function __toString() { 240 | return "exception '".__CLASS__ ."' with message '".$this->getMessage()."' in ".$this->getFile().":".$this->getLine()."\nStack trace:\n".$this->getTraceAsString(); 241 | } 242 | } --------------------------------------------------------------------------------