PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/bitly.php

https://github.com/igorescobar/Bitly-PHP
PHP | 411 lines | 102 code | 102 blank | 207 comment | 19 complexity | 47ca332415ee64e0b817f2f5d92965a9 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2010 Igor Escobar, Bitly-PHP
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * -----------------------------------------------------------------------------
  28. *
  29. * PHP Library to use the RESTfull API of Bit.ly
  30. *
  31. * @author Igor Escobar (blog@igorescobar.com)
  32. *
  33. * ----- ATENCAO -----
  34. *
  35. * If possible, always work with the json return.
  36. * The PHP has natively the functions to care of this type of data.
  37. * If you want use a XML return, the library was return the entire response data in the XML format,
  38. * then you have to use a XML Parser that you wanna use.
  39. *
  40. * -------------------
  41. *
  42. * @uses
  43. * http://wiki.github.com/igorescobar/Bitly-PHP/
  44. *
  45. */
  46. class Bitly {
  47. /**
  48. * Api version that you want use.
  49. *
  50. * @var string
  51. */
  52. public $version = '2.0.1';
  53. /**
  54. * Bit.ly Login
  55. *
  56. * @var string
  57. */
  58. public $login = 'login_bitly';
  59. /**
  60. * Api-key to acess the bit.ky API.
  61. *
  62. * @var string
  63. */
  64. public $api_key = 'api_key';
  65. /**
  66. * API out-put format .
  67. *
  68. * @uses JSON
  69. * @uses XML
  70. * @var string
  71. */
  72. public $format = 'json';
  73. /**
  74. * Callback function. It's optional. If you wanna use, fill the function name that you
  75. * want call.
  76. *
  77. * @var string
  78. */
  79. public $callback;
  80. /**
  81. * Url that you want to use on the bit.ly API.
  82. *
  83. * @var string
  84. */
  85. public $url;
  86. /**
  87. * To set when the API was already invoked.
  88. *
  89. * @var boolean
  90. */
  91. protected $active = false;
  92. /**
  93. * Just in case the Bit.ly API fail.
  94. *
  95. * @var boolean
  96. */
  97. protected $fail = false;
  98. /**
  99. * Action that the library was execute
  100. *
  101. * @var string
  102. */
  103. protected $action = null;
  104. public function __construct ( $login = null, $api_key = null ) {
  105. // Force the lower-case format
  106. $this->format = strtolower( $this->format );
  107. /**
  108. * If you prefer you also can use the Bitly Library like this:
  109. * $bit = new Bitly('<your_login>', '<your_api_key>');
  110. */
  111. $this->login = ( !is_null ( $login ) ) ? $login : $this->login;
  112. $this->api_key = ( !is_null ( $login ) ) ? $api_key : $this->api_key;
  113. }
  114. /**
  115. * Convert data from bit.ly API
  116. *
  117. * @return void
  118. * @author Igor Escobar
  119. */
  120. public function get (){
  121. if( $this->format == 'json' ) {
  122. if ( !is_object ( $this->return ) )
  123. $this->return = json_decode( $this->return );
  124. if($this->return->statusCode == 'ERROR')
  125. $this->fail = true;
  126. else
  127. $this->fail = false;
  128. }
  129. }
  130. /**
  131. * Function responsible to read what the class have to do on the bit.ly API
  132. * and make that simple.
  133. *
  134. * @param $action - action to perform on bit.ly
  135. * @return void
  136. * @author Igor Escobar
  137. */
  138. private function action ( $action ) {
  139. $this->action = $action;
  140. $this->active = false;
  141. /**
  142. * Create the packet that was sent to the Bit.ly API
  143. */
  144. $params = http_build_query ( array(
  145. 'version' => $this->version,
  146. 'login' => $this->login,
  147. 'apiKey' => $this->api_key,
  148. 'longUrl' => $this->url,
  149. 'shortUrl' => $this->url,
  150. 'format' => $this->format,
  151. 'callback' => $this->callback
  152. ) );
  153. // Make a requisition to the Bit.ly API
  154. $this->return = $this->get_file_contents ( 'http://api.bit.ly/' . $this->action . '?' . $params );
  155. // Take care of the response
  156. $this->get();
  157. }
  158. /**
  159. * Execute the Bit.ly shorten method
  160. *
  161. * @author Igor Escobar
  162. */
  163. public function shorten ( $url = null ) {
  164. /**
  165. * Just i case if you wanna invoke this method like this:
  166. * $bitly->shorten ( '<long_url>' );
  167. */
  168. $this->url = ( !is_null( $url ) ) ? $url : $this->url;
  169. // Inform the action type that you need execute
  170. $this->action('shorten');
  171. /**
  172. * Shortcut if you wanna read the shortened url directly when you
  173. * print the method.
  174. */
  175. return $this->getData()->shortUrl;
  176. }
  177. /**
  178. * Execute the expand method directly on Bit.ly
  179. *
  180. * @author Igor Escobar
  181. */
  182. public function expand ( $url = null ) {
  183. /**
  184. * Just i case if you wanna invoke this method like this:
  185. * $bitly->shorten ( '<short_url>' );
  186. */
  187. $this->url = ( !is_null( $url ) ) ? $url : $this->url;
  188. // Inform the action type that you need execute
  189. $this->action('expand');
  190. /**
  191. * Shortcut if you wanna read the shortened url directly when you
  192. * print the method.
  193. */
  194. return $this->getData()->longUrl;
  195. }
  196. /**
  197. * Executa o Info do Bit.ly
  198. *
  199. * @author Igor Escobar
  200. */
  201. public function info ( $url = null ) {
  202. /**
  203. * Just i case if you wanna invoke this method like this:
  204. * $bitly->shorten ( '<short_url>' );
  205. */
  206. $this->url = ( !is_null( $url ) ) ? $url : $this->url;
  207. // Inform the action type that you need execute
  208. $this->action('info');
  209. /**
  210. * Shortcut if you wanna read the shortened url directly when you
  211. * print the method.
  212. */
  213. return $this->getData();
  214. }
  215. /**
  216. * Executa o Stats do Bit.ly
  217. *
  218. * @author Igor Escobar
  219. */
  220. public function stats ( $url = null ) {
  221. /**
  222. * Just i case if you wanna invoke this method like this:
  223. * $bitly->shorten ( '<short_url>' );
  224. */
  225. $this->url = ( !is_null( $url ) ) ? $url : $this->url;
  226. // Inform the action type that you need execute
  227. $this->action('stats');
  228. /**
  229. * Shortcut if you wanna read the shortened url directly when you
  230. * print the method.
  231. */
  232. return $this->getData();
  233. }
  234. /**
  235. * Use this function if you wanna read any parameter from the response bit.ly result.
  236. *
  237. * @return void
  238. * @author Igor Escobar
  239. */
  240. public function getData() {
  241. // If the RESTful API was invoked, then i will proceed.
  242. if ( $this->active != false )
  243. return false;
  244. if ( $this->format == 'json' ) {
  245. if ( $this->fail != true ) {
  246. /**
  247. * In some cases the bit.ly return a diferent array. Then i have
  248. * to develop a exit to do this method work correctly on all function
  249. * cases. The solution is get always the first parameter from the
  250. * 'result' parameter from bit.ly.
  251. */
  252. $ar_object_vars = get_object_vars ( $this->return->results );
  253. $ar_object_keys = array_keys ( $ar_object_vars );
  254. $node = $ar_object_keys[0];
  255. // Stats have return the response on a diferent node.
  256. if ( $this->action != 'stats' )
  257. return $this->return->results->$node;
  258. else
  259. return $this->return->results;
  260. } else {
  261. // Debug is activated
  262. $this->debug();
  263. }
  264. /**
  265. * If you need a XML return i always will return the original reponse from bit.ly.
  266. * Then will have to use a XML Parser that you prefer.
  267. */
  268. } elseif ( $formato == 'xml' ) {
  269. return $this->return;
  270. }
  271. }
  272. /**
  273. * This functions is responsible to make the requisition on the Bit.ly server.
  274. * By benchmarking propouses the class always will use the cURL to make all requisitions.
  275. *
  276. * 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.
  277. *
  278. * @param string $url
  279. * @return stream-response
  280. * @author Igor Escobar
  281. */
  282. private function get_file_contents ( $url ) {
  283. if ( function_exists( 'curl_init' ) ) {
  284. $curl = curl_init ();
  285. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  286. curl_setopt ( $curl, CURLOPT_URL, $url );
  287. $contents = curl_exec ( $curl );
  288. curl_close ( $curl );
  289. if ( $contents )
  290. return $contents;
  291. else
  292. return false;
  293. } else {
  294. return file_get_contents ( $url );
  295. }
  296. }
  297. /**
  298. * If anything gone wrong, call this functions to discover what the fuck is going on.
  299. *
  300. * @return void
  301. * @author Igor Escobar
  302. */
  303. public function debug () {
  304. echo "<pre>";
  305. print_r( $this->return );
  306. echo "</pre>";
  307. }
  308. }
  309. ?>