/libraries/Bitly.php

https://github.com/TheRegge/bit.ly-Library-for-CodeIgniter · PHP · 266 lines · 154 code · 29 blank · 83 comment · 38 complexity · c327dd80cd092eb853ec969013d36b52 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * bit.ly REST API v3 library for CodeIgniter
  4. *
  5. * @license Creative Commons Attribution 3.0 <http://creativecommons.org/licenses/by/3.0/>
  6. * @version 1.0
  7. * @author Patrick Popowicz <http://patrickpopowicz.com>
  8. * @copyright Copyright (c) 2010, Patrick Popowicz <http://patrickpopowicz.com>
  9. */
  10. class Bitly {
  11. private $CI; // CodeIgniter instance
  12. // bit.ly API parameters
  13. private $bitly_login; // bit.ly Login name
  14. private $bitly_apiKey; // bit.ly API Key
  15. private $bitly_x_login; // End-user's login when make requests on behalf of another bit.ly user.
  16. private $bitly_x_apiKey; // End-user's apiKey when make requests on behalf of another bit.ly user.
  17. private $bitly_format; // Requested response format. Supported formats: json (default), xml, txt.
  18. private $bitly_domain; // Refers to a preferred output domain; either "bit.ly" (default) or "j.mp".
  19. // bit.ly API variables
  20. private $api = "http://api.bit.ly/v3/"; // bit.ly API target URL
  21. private $response; // bit.ly response
  22. function __construct($params = array())
  23. {
  24. $this->CI =& get_instance();
  25. log_message('debug', 'bit.ly Class Initialized');
  26. $this->initialize($params);
  27. }
  28. // Initializes the library parameters
  29. public function initialize($params = array())
  30. {
  31. $this->response = '';
  32. // Set API preferences from the config file if they are not passed in the $params array
  33. foreach (array('bitly_login','bitly_apiKey','bitly_x_login','bitly_x_apiKey','bitly_format','bitly_domain') as $key)
  34. {
  35. $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
  36. }
  37. }
  38. /**
  39. * Shortens a URL
  40. *
  41. * @param string $longUrl Target url to be shortened
  42. * @param bool $verbose Output flag
  43. * @return mixed (!verbose) string | (verbose) array
  44. * @author Patrick Popowicz
  45. */
  46. public function shorten($longUrl, $verbose = FALSE)
  47. {
  48. // Make sure all of the required parameters are set and longUrl is a URL
  49. if (isset($longUrl) AND filter_var($longUrl, FILTER_VALIDATE_URL))
  50. {
  51. $params = array(
  52. 'longUrl' => trim($longUrl),
  53. 'format' => $this->bitly_format,
  54. 'domain' => $this->bitly_domain
  55. );
  56. if ($this->bitly_x_login && $this->bitly_x_apiKey)
  57. {
  58. $params['x_login'] = $this->bitly_x_login;
  59. $params['x_apiKey'] = $this->bitly_x_apiKey;
  60. }
  61. if ($this->_execute('shorten', $params))
  62. {
  63. return ($verbose || $this->bitly_format != 'json') ? $this->response : $this->response['data']['url'];
  64. }
  65. else { return FALSE; }
  66. }
  67. else { return FALSE; }
  68. }
  69. /**
  70. * Expands a bit.ly shortUrl or hash
  71. *
  72. * @param array $targets Target shortUrls or hashes to expand in numerically indexed array
  73. * @param bool $verbose Output flag
  74. * @return mixed (format != json) string | (format == json) array
  75. * @author Patrick Popowicz
  76. */
  77. public function expand($targets = array(), $verbose = FALSE)
  78. {
  79. // Check the targets and build the params array
  80. if (count($targets))
  81. {
  82. foreach ($targets as $key => $value)
  83. {
  84. $url = parse_url($value);
  85. if (!isset($url['host']) && !preg_match('/.ly|.mp/i',$url['path']))
  86. {
  87. // Target is a hash
  88. $params['hash'][] = $value;
  89. }
  90. else
  91. {
  92. // Target is a shortUrl, make sure we have a full url
  93. $params['shortUrl'][] = (isset($url['scheme'])) ? $value : 'http://'.$value;
  94. }
  95. }
  96. $params['format'] = $this->bitly_format;
  97. if ($this->_execute('expand', $params))
  98. {
  99. // Determine what to return
  100. return ($verbose) ? $this->response : ((count($targets) == 1 && $this->bitly_format == 'json') ? $this->response['data']['expand'][0]['long_url'] : $this->response);
  101. }
  102. else { return FALSE; }
  103. }
  104. else { return FALSE; }
  105. }
  106. /**
  107. * Validates a 3rd party login and apiKey pair
  108. *
  109. * @param array $params 3rd party login and apiKey
  110. * @return mixed (format != json || txt) string | (format == json || txt) bool
  111. * @author Patrick Popowicz
  112. */
  113. public function validate($params = array(), $verbose = FALSE)
  114. {
  115. // Check the targets and build the params array
  116. if (count($params))
  117. {
  118. $params['format'] = $this->bitly_format;
  119. if ($this->_execute('validate', $params))
  120. {
  121. // Determine what to return
  122. return ($verbose) ? $this->response : (($this->bitly_format == 'json') ? $this->response['data']['valid'] : $this->response);
  123. }
  124. else { return FALSE; }
  125. }
  126. else { return FALSE; }
  127. }
  128. /**
  129. * Returns click information for one or more shortUrls or hashes
  130. *
  131. * @param array $targets Target shortUrls or hashes to expand in numerically indexed array
  132. * @param string $type type of click returned, either user (passed url or hash) or global
  133. * @param bool $verbose Output flag
  134. * @return mixed (format != json) string | (format == json) array
  135. * @author Patrick Popowicz
  136. */
  137. public function clicks($targets = array(), $type = 'user', $verbose = FALSE)
  138. {
  139. // Check the targets and build the params array
  140. if (count($targets))
  141. {
  142. foreach ($targets as $key => $value)
  143. {
  144. $url = parse_url($value);
  145. if (!isset($url['host']) && !preg_match('/.ly|.mp/i',$url['path']))
  146. {
  147. // Target is a hash
  148. $params['hash'][] = $value;
  149. }
  150. else
  151. {
  152. // Target is a shortUrl, make sure we have a full url
  153. $params['shortUrl'][] = (isset($url['scheme'])) ? $value : 'http://'.$value;
  154. }
  155. }
  156. $params['format'] = $this->bitly_format;
  157. if ($this->_execute('clicks', $params))
  158. {
  159. // Determine what to return
  160. return ($verbose) ? $this->response : ((count($targets) == 1 && $this->bitly_format == 'json') ? $this->response['data']['clicks'][0][$type.'_clicks'] : $this->response);
  161. }
  162. else { return FALSE; }
  163. }
  164. else { return FALSE; }
  165. }
  166. /**
  167. * Validates a Pro Domain
  168. *
  169. * @param string bit.ly pro domain
  170. * @return mixed (format != json) string | (format == json) bool
  171. * @author Patrick Popowicz
  172. */
  173. public function pro_domain($domain = '', $verbose = FALSE)
  174. {
  175. // Check the targets and build the params array
  176. $params['domain'] = $domain;
  177. $params['format'] = $this->bitly_format;
  178. if ($this->_execute('bitly_pro_domain', $params))
  179. {
  180. // Determine what to return
  181. return ($verbose) ? $this->response : (($this->bitly_format == 'json') ? $this->response['data']['bitly_pro_domain'] : $this->response);
  182. }
  183. else { return FALSE; }
  184. }
  185. /**
  186. * Executes the API request using cURL
  187. *
  188. * @param string $method API method being used
  189. * @param bool $verbose Output flag
  190. * @return bool
  191. * @author Patrick Popowicz
  192. */
  193. private function _execute($method, $params)
  194. {
  195. // Add in the primary login and apiKey
  196. $params = array_merge(array('login' => $this->bitly_login, 'apiKey' => $this->bitly_apiKey), $params);
  197. // Create the argument string
  198. $target = $this->api . $method . '?';
  199. foreach ($params as $key => $value)
  200. {
  201. if (!is_array($value))
  202. {
  203. $target .= http_build_query(array($key => $value)) . '&';
  204. }
  205. else
  206. {
  207. foreach ($value as $sub)
  208. {
  209. $target .= http_build_query(array($key => $sub)) . '&';
  210. }
  211. }
  212. }
  213. // Use cURL to fetch
  214. $curl = curl_init();
  215. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  216. curl_setopt($curl, CURLOPT_URL, $target);
  217. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  218. if ($response = curl_exec($curl))
  219. {
  220. $this->response = ($this->bitly_format == 'json') ? json_decode($response, TRUE) : $response;
  221. return TRUE;
  222. }
  223. else { return FALSE; }
  224. }
  225. /**
  226. * Returns the response value
  227. *
  228. * @return mixed
  229. * @author Patrick Popowicz
  230. */
  231. public function response()
  232. {
  233. return $this->response;
  234. }
  235. }
  236. /* End of file Bitly.php */
  237. /* Location: ./application/libraries/Bitly.php */