PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/util/bitly.class.php

https://github.com/tunn/opTextBoxPlugin
PHP | 223 lines | 170 code | 31 blank | 22 comment | 14 complexity | b7f5d04a69d38dc86294361df0f5e3f8 MD5 | raw file
  1. <?php
  2. /*
  3. Author: Ruslanas Balčiūnas
  4. http://bitly.googlecode.com
  5. Usage:
  6. $bitly = new Bitly($login, $apiKey);
  7. $short = $bitly->shortenSingle('http://bitly.googlecode.com');
  8. $long = $bitly->expandSingle($short);
  9. print_r( $bitly->getStatsArray($short));
  10. print_r( $bitly->getInfoArray($long));
  11. */
  12. class Bitly {
  13. protected $api = 'http://api.bit.ly/';
  14. private $format = 'json';
  15. private $version = '2.0.1';
  16. private $validActions = array(
  17. 'shorten',
  18. 'stats',
  19. 'info',
  20. 'expand'
  21. );
  22. public function Bitly($login, $apiKey)
  23. {
  24. $this->login = $login;
  25. $this->apiKey = $apiKey;
  26. $this->statusCode = 'OK';
  27. $this->errorMessage = '';
  28. $this->errorCode = '';
  29. return true;
  30. }
  31. private function setError($message, $code = 101)
  32. {
  33. $this->errorCode = $code;
  34. $this->errorMessage = $message;
  35. $this->statusCode = 'ERROR';
  36. }
  37. public function validAction($action)
  38. {
  39. if( in_array($action, $this->validActions)) {
  40. return true;
  41. }
  42. $this->setError("Undefined method $action", 202);
  43. return false;
  44. }
  45. public function error()
  46. {
  47. $ret = array(
  48. "errorCode" => $this->errorCode,
  49. "errorMessage" => $this->errorMessage,
  50. "statusCode" => $this->statusCode
  51. );
  52. // Function used for passing empty result sometimes.
  53. if( $this->statusCode == 'OK') {
  54. $ret['results'] = array();
  55. }
  56. if( $this->format == 'json') {
  57. return json_encode($ret);
  58. } else {
  59. throw new Exception('Unsupported format');
  60. }
  61. }
  62. public function shorten($message)
  63. {
  64. $postFields = '';
  65. preg_match_all("/http(s?):\/\/[^( |$|\]|,|\\\)]+/i", $message, $matches);
  66. for($i=0;$i<sizeof( $matches[0]);$i++) {
  67. $curr = $matches[0][$i];
  68. // ignore bitly urls
  69. if( !strstr($curr, 'http://bit.ly')) {
  70. $postFields .= '&longUrl=' . urlencode( $curr);
  71. }
  72. }
  73. // nothing to shorten, return empty result
  74. if( !strlen($postFields)) {
  75. return $this->error();
  76. }
  77. return $this->process('shorten', $postFields);
  78. }
  79. public function expand($message)
  80. {
  81. $postFields = '&hash=' . $this->getHash($message);
  82. return $this->process('expand', $postFields);
  83. }
  84. public function info($bitlyUrl)
  85. {
  86. $hash = $this->getHash($bitlyUrl);
  87. $postFields = '&hash=' . $hash;
  88. return $this->process('info', $postFields);
  89. }
  90. public function stats($bitlyUrl)
  91. {
  92. // Take only first hash or url. Ignore others.
  93. $a = split(',', $bitlyUrl);
  94. $postFields = '&hash=' . $this->getHash($a[0]);
  95. return $this->process('stats', $postFields);
  96. }
  97. protected function process($action, $postFields) {
  98. $ch = curl_init( $this->api . $action);
  99. $postFields = 'version=' . $this->version . $postFields;
  100. $postFields .= '&format=' . $this->format;
  101. $postFields .= '&history=1';
  102. curl_setopt($ch, CURLOPT_USERPWD, $this->login . ':' . $this->apiKey);
  103. curl_setopt($ch, CURLOPT_POST, 1);
  104. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  105. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  106. $response = curl_exec($ch);
  107. curl_close($ch);
  108. return $response;
  109. }
  110. public function setReturnFormat($format)
  111. {
  112. // needed for restoration
  113. $this->oldFormat = $this->format;
  114. $this->format = $format;
  115. return $this->format;
  116. }
  117. private function restoreFormat()
  118. {
  119. if( !empty( $this->oldFormat)) {
  120. $this->format = $this->oldFormat;
  121. }
  122. return $this->format;
  123. }
  124. // expect url, shortened url or hash
  125. public function getHash($message)
  126. {
  127. // if url and not bit.ly get shortened first
  128. if( strstr($message, 'http://') && !strstr($message, 'http://bit.ly')) {
  129. $message = $this->shortenSingle($message);
  130. }
  131. $hash = str_replace('http://bit.ly/', '', $message);
  132. return $hash;
  133. }
  134. public function shortenSingle($message)
  135. {
  136. $this->setReturnFormat('json');
  137. $data = json_decode( $this->shorten($message), true);
  138. // return to previous state.
  139. $this->restoreFormat();
  140. // replace every long url with short one
  141. foreach($data['results'] as $url => $d) {
  142. $message = str_replace($url, $d['shortUrl'], $message);
  143. }
  144. return $message;
  145. }
  146. public function expandSingle($shortUrl)
  147. {
  148. $this->setReturnFormat('json');
  149. $data = json_decode( $this->expand($shortUrl), true);
  150. $this->restoreFormat();
  151. return $data['results'][ $this->getHash($shortUrl)]['longUrl'];
  152. }
  153. public function getInfoArray($url)
  154. {
  155. $this->setReturnFormat('json');
  156. $json = $this->info($url);
  157. $this->restoreFormat();
  158. $data = json_decode($json, true);
  159. $this->infoArray = array_pop( $data['results']);
  160. return $this->infoArray;
  161. }
  162. public function getStatsArray($url)
  163. {
  164. $this->setReturnFormat('json');
  165. $json = $this->stats($url);
  166. $this->restoreFormat();
  167. $data = json_decode($json, true);
  168. $this->statsArray = $data['results'];
  169. return $this->statsArray;
  170. }
  171. public function getClicks()
  172. {
  173. return $this->statsArray['clicks'];
  174. }
  175. // get thumbnail (small, middle, large)
  176. public function getThumbnail($size = 'small')
  177. {
  178. if( !in_array($size, array('small', 'medium', 'large'))) {
  179. throw new Exception('Invalid size value');
  180. }
  181. if( empty( $this->infoArray)) {
  182. throw new Exception('Info not loaded');
  183. }
  184. return $this->infoArray['thumbnail'][$size];
  185. }
  186. public function getTitle()
  187. {
  188. return $this->infoArray['htmlTitle'];
  189. }
  190. }