├── README ├── bitly.php ├── examples ├── expand-urls.php ├── get-info-urls.php ├── shorten-urls.php └── statistics-url.php └── license.txt /README: -------------------------------------------------------------------------------- 1 | * Classe PHP para utilização da API RESTful do Bit.ly 2 | * 3 | * @author Igor Escobar (blog@igorescobar.com) 4 | * 5 | * @thanks-to Fernando Mantoan (contato@fernandomantoan.com) 6 | * --- Encontrou bugs, propos melhorias e melhorias no código. 7 | * 8 | * @functions shorten 9 | * @functions expand 10 | * @functions info 11 | * @functions stats 12 | * @functions getData 13 | * @functions debug 14 | * 15 | * ----- ATENCAO ----- 16 | * 17 | * Para maiores informações sobre o seu uso, veja a Wiki ( http://wiki.github.com/igorescobar/Bitly-PHP/ ) 18 | * do projeto ou o arquivo examples.php -------------------------------------------------------------------------------- /bitly.php: -------------------------------------------------------------------------------- 1 | format = strtolower( $this->format ); 128 | 129 | /** 130 | * If you prefer you also can use the Bitly Library like this: 131 | * $bit = new Bitly('', ''); 132 | */ 133 | 134 | $this->login = ( !is_null ( $login ) ) ? $login : $this->login; 135 | $this->api_key = ( !is_null ( $login ) ) ? $api_key : $this->api_key; 136 | 137 | 138 | } 139 | 140 | /** 141 | * Convert data from bit.ly API 142 | * 143 | * @return void 144 | * @author Igor Escobar 145 | */ 146 | 147 | public function get (){ 148 | 149 | if( $this->format == 'json' ) { 150 | 151 | if ( !is_object ( $this->return ) ) 152 | $this->return = json_decode( $this->return ); 153 | 154 | if($this->return->statusCode == 'ERROR') 155 | $this->fail = true; 156 | else 157 | $this->fail = false; 158 | 159 | } 160 | 161 | } 162 | 163 | /** 164 | * Function responsible to read what the class have to do on the bit.ly API 165 | * and make that simple. 166 | * 167 | * @param $action - action to perform on bit.ly 168 | * @return void 169 | * @author Igor Escobar 170 | */ 171 | 172 | private function action ( $action ) { 173 | 174 | $this->action = $action; 175 | $this->active = false; 176 | 177 | /** 178 | * Create the packet that was sent to the Bit.ly API 179 | */ 180 | 181 | $params = http_build_query ( array( 182 | 'version' => $this->version, 183 | 'login' => $this->login, 184 | 'apiKey' => $this->api_key, 185 | 'longUrl' => $this->url, 186 | 'shortUrl' => $this->url, 187 | 'format' => $this->format, 188 | 'callback' => $this->callback 189 | ) ); 190 | 191 | // Make a requisition to the Bit.ly API 192 | $this->return = $this->get_file_contents ( 'http://api.bit.ly/' . $this->action . '?' . $params ); 193 | 194 | // Take care of the response 195 | $this->get(); 196 | 197 | } 198 | 199 | /** 200 | * Execute the Bit.ly shorten method 201 | * 202 | * @author Igor Escobar 203 | */ 204 | 205 | public function shorten ( $url = null ) { 206 | 207 | /** 208 | * Just i case if you wanna invoke this method like this: 209 | * $bitly->shorten ( '' ); 210 | */ 211 | 212 | $this->url = ( !is_null( $url ) ) ? $url : $this->url; 213 | 214 | // Inform the action type that you need execute 215 | $this->action('shorten'); 216 | 217 | /** 218 | * Shortcut if you wanna read the shortened url directly when you 219 | * print the method. 220 | */ 221 | 222 | return $this->getData()->shortUrl; 223 | 224 | } 225 | 226 | /** 227 | * Execute the expand method directly on Bit.ly 228 | * 229 | * @author Igor Escobar 230 | */ 231 | 232 | public function expand ( $url = null ) { 233 | 234 | /** 235 | * Just i case if you wanna invoke this method like this: 236 | * $bitly->shorten ( '' ); 237 | */ 238 | 239 | $this->url = ( !is_null( $url ) ) ? $url : $this->url; 240 | 241 | // Inform the action type that you need execute 242 | $this->action('expand'); 243 | 244 | /** 245 | * Shortcut if you wanna read the shortened url directly when you 246 | * print the method. 247 | */ 248 | 249 | return $this->getData()->longUrl; 250 | 251 | } 252 | 253 | /** 254 | * Executa o Info do Bit.ly 255 | * 256 | * @author Igor Escobar 257 | */ 258 | 259 | public function info ( $url = null ) { 260 | 261 | /** 262 | * Just i case if you wanna invoke this method like this: 263 | * $bitly->shorten ( '' ); 264 | */ 265 | 266 | $this->url = ( !is_null( $url ) ) ? $url : $this->url; 267 | 268 | // Inform the action type that you need execute 269 | $this->action('info'); 270 | 271 | /** 272 | * Shortcut if you wanna read the shortened url directly when you 273 | * print the method. 274 | */ 275 | 276 | return $this->getData(); 277 | 278 | 279 | } 280 | 281 | /** 282 | * Executa o Stats do Bit.ly 283 | * 284 | * @author Igor Escobar 285 | */ 286 | 287 | public function stats ( $url = null ) { 288 | 289 | /** 290 | * Just i case if you wanna invoke this method like this: 291 | * $bitly->shorten ( '' ); 292 | */ 293 | 294 | $this->url = ( !is_null( $url ) ) ? $url : $this->url; 295 | 296 | // Inform the action type that you need execute 297 | $this->action('stats'); 298 | 299 | /** 300 | * Shortcut if you wanna read the shortened url directly when you 301 | * print the method. 302 | */ 303 | 304 | return $this->getData(); 305 | 306 | 307 | } 308 | 309 | /** 310 | * Use this function if you wanna read any parameter from the response bit.ly result. 311 | * 312 | * @return void 313 | * @author Igor Escobar 314 | */ 315 | 316 | public function getData() { 317 | 318 | // If the RESTful API was invoked, then i will proceed. 319 | if ( $this->active != false ) 320 | return false; 321 | 322 | if ( $this->format == 'json' ) { 323 | 324 | if ( $this->fail != true ) { 325 | 326 | /** 327 | * In some cases the bit.ly return a diferent array. Then i have 328 | * to develop a exit to do this method work correctly on all function 329 | * cases. The solution is get always the first parameter from the 330 | * 'result' parameter from bit.ly. 331 | */ 332 | 333 | $ar_object_vars = get_object_vars ( $this->return->results ); 334 | $ar_object_keys = array_keys ( $ar_object_vars ); 335 | $node = $ar_object_keys[0]; 336 | 337 | // Stats have return the response on a diferent node. 338 | 339 | if ( $this->action != 'stats' ) 340 | return $this->return->results->$node; 341 | else 342 | return $this->return->results; 343 | 344 | } else { 345 | 346 | // Debug is activated 347 | $this->debug(); 348 | } 349 | 350 | /** 351 | * If you need a XML return i always will return the original reponse from bit.ly. 352 | * Then will have to use a XML Parser that you prefer. 353 | */ 354 | 355 | } elseif ( $formato == 'xml' ) { 356 | 357 | return $this->return; 358 | 359 | } 360 | 361 | } 362 | 363 | /** 364 | * This functions is responsible to make the requisition on the Bit.ly server. 365 | * By benchmarking propouses the class always will use the cURL to make all requisitions. 366 | * 367 | * Case you don't have the cURL extensions on your server, then i will use the file_get_contents (slow) to make it for you. 368 | * 369 | * @param string $url 370 | * @return stream-response 371 | * @author Igor Escobar 372 | */ 373 | 374 | private function get_file_contents ( $url ) { 375 | 376 | if ( function_exists( 'curl_init' ) ) { 377 | 378 | $curl = curl_init (); 379 | curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); 380 | curl_setopt ( $curl, CURLOPT_URL, $url ); 381 | $contents = curl_exec ( $curl ); 382 | curl_close ( $curl ); 383 | 384 | if ( $contents ) 385 | return $contents; 386 | else 387 | return false; 388 | 389 | } else { 390 | return file_get_contents ( $url ); 391 | } 392 | 393 | } 394 | 395 | /** 396 | * If anything gone wrong, call this functions to discover what the fuck is going on. 397 | * 398 | * @return void 399 | * @author Igor Escobar 400 | */ 401 | 402 | public function debug () { 403 | 404 | echo "
"; 
405 | 		print_r( $this->return ); 
406 | 		echo "
"; 407 | 408 | } 409 | 410 | } 411 | ?> -------------------------------------------------------------------------------- /examples/expand-urls.php: -------------------------------------------------------------------------------- 1 | ',''); 6 | echo $bitly->expand('http://bit.ly/b6R4Uf'); // shortcut to print the long url 7 | 8 | // Detailed return 9 | 10 | print_r ( $bitly->getData () ); 11 | 12 | ?> -------------------------------------------------------------------------------- /examples/get-info-urls.php: -------------------------------------------------------------------------------- 1 | ',''); 6 | print_r ( $bitly->info ( 'http://bit.ly/b6R4Uf' ) ); 7 | 8 | ?> -------------------------------------------------------------------------------- /examples/shorten-urls.php: -------------------------------------------------------------------------------- 1 | ',''); 6 | echo $bitly->shorten('http://www.google.com/'); // shortcut to print the shorten url 7 | 8 | // Detailed return 9 | 10 | print_r ( $bitly->getData () ); 11 | 12 | ?> -------------------------------------------------------------------------------- /examples/statistics-url.php: -------------------------------------------------------------------------------- 1 | ',''); 6 | print_r ( $bitly->stats ( 'http://bit.ly/b6R4Uf' ) ); 7 | 8 | ?> -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Igor Escobar, Bitly-PHP 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. --------------------------------------------------------------------------------