PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/bitly.php

http://github.com/Falicon/BitlyPHP
PHP | 223 lines | 106 code | 22 blank | 95 comment | 5 complexity | bd6193d02289d889fbb1e1d41fad111c MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Simple PHP library for interacting with the v3 bit.ly api (only deals with
  5. * JSON format, but supports new OAuth endpoints).
  6. * REQUIREMENTS: PHP, Curl, JSON
  7. *
  8. * @link https://github.com/Falicon/BitlyPHP
  9. * @author Kevin Marshall <info@falicon.com>
  10. */
  11. /**
  12. * The URI of the standard bitly v3 API.
  13. */
  14. define('bitly_api', 'http://api.bit.ly/v3/');
  15. /**
  16. * The URI of the bitly OAuth endpoints.
  17. */
  18. define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');
  19. /**
  20. * The URI for OAuth access token requests.
  21. */
  22. define('bitly_oauth_access_token', 'https://api-ssl.bit.ly/oauth/');
  23. /**
  24. * Returns an OAuth access token as well as API users for a given code.
  25. *
  26. * @param $code
  27. * The OAuth verification code acquired via OAuth’s web authentication
  28. * protocol.
  29. * @param $redirect
  30. * The page to which a user was redirected upon successfully authenticating.
  31. * @param $client_id
  32. * The client_id assigned to your OAuth app. (http://bit.ly/a/account)
  33. * @param $client_secret
  34. * The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
  35. *
  36. * @return
  37. * An associative array containing:
  38. * - login: The corresponding bit.ly users username.
  39. * - api_key: The corresponding bit.ly users API key.
  40. * - access_token: The OAuth access token for specified user.
  41. *
  42. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
  43. */
  44. function bitly_oauth_access_token($code, $redirect, $client_id, $client_secret) {
  45. $results = array();
  46. $url = bitly_oauth_access_token . "access_token";
  47. $params = array();
  48. $params['client_id'] = $client_id;
  49. $params['client_secret'] = $client_secret;
  50. $params['code'] = $code;
  51. $params['redirect_uri'] = $redirect;
  52. $output = bitly_post_curl($url, $params);
  53. $parts = explode('&', $output);
  54. foreach ($parts as $part) {
  55. $bits = explode('=', $part);
  56. $results[$bits[0]] = $bits[1];
  57. }
  58. return $results;
  59. }
  60. /**
  61. * Returns an OAuth access token via the user's bit.ly login Username and Password
  62. *
  63. * @param $username
  64. * The user's Bitly username
  65. * @param $password
  66. * The user's Bitly password
  67. * @param $client_id
  68. * The client_id assigned to your OAuth app. (http://bit.ly/a/account)
  69. * @param $client_secret
  70. * The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
  71. *
  72. * @return
  73. * An associative array containing:
  74. * - access_token: The OAuth access token for specified user.
  75. *
  76. */
  77. function bitly_oauth_access_token_via_password($username, $password, $client_id, $client_secret) {
  78. $results = array();
  79. $url = bitly_oauth_access_token . "access_token";
  80. $headers = array();
  81. $headers[] = 'Authorization: Basic '.base64_encode($client_id . ":" . $client_secret);
  82. $params = array();
  83. $params['grant_type'] = "password";
  84. $params['username'] = $username;
  85. $params['password'] = $password;
  86. $output = bitly_post_curl($url, $params, $headers);
  87. $decoded_output = json_decode($output,1);
  88. $results = array(
  89. "access_token" => $decoded_output['access_token']
  90. );
  91. return $results;
  92. }
  93. /**
  94. * Format a GET call to the bit.ly API.
  95. *
  96. * @param $endpoint
  97. * bit.ly API endpoint to call.
  98. * @param $params
  99. * associative array of params related to this call.
  100. * @param $complex
  101. * set to true if params includes associative arrays itself (or using <php5)
  102. *
  103. * @return
  104. * associative array of bit.ly response
  105. *
  106. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/validate
  107. */
  108. function bitly_get($endpoint, $params, $complex=false) {
  109. $result = array();
  110. if ($complex) {
  111. $url_params = "";
  112. foreach ($params as $key => $val) {
  113. if (is_array($val)) {
  114. // we need to flatten this into one proper command
  115. $recs = array();
  116. foreach ($val as $rec) {
  117. $tmp = explode('/', $rec);
  118. $tmp = array_reverse($tmp);
  119. array_push($recs, $tmp[0]);
  120. }
  121. $val = implode('&' . $key . '=', $recs);
  122. }
  123. $url_params .= '&' . $key . "=" . $val;
  124. }
  125. $url = bitly_oauth_api . $endpoint . "?" . substr($url_params, 1);
  126. } else {
  127. $url = bitly_oauth_api . $endpoint . "?" . http_build_query($params);
  128. }
  129. //echo $url . "\n";
  130. $result = json_decode(bitly_get_curl($url), true);
  131. return $result;
  132. }
  133. /**
  134. * Format a POST call to the bit.ly API.
  135. *
  136. * @param $uri
  137. * URI to call.
  138. * @param $fields
  139. * Array of fields to send.
  140. */
  141. function bitly_post($endpoint, $params) {
  142. $result = array();
  143. $url = bitly_oauth_api . $api_endpoint;
  144. $output = json_decode(bitly_post_curl($url, $params), true);
  145. $result = $output['data'][str_replace('/', '_', $api_endpoint)];
  146. $result['status_code'] = $output['status_code'];
  147. return $result;
  148. }
  149. /**
  150. * Make a GET call to the bit.ly API.
  151. *
  152. * @param $uri
  153. * URI to call.
  154. */
  155. function bitly_get_curl($uri) {
  156. $output = "";
  157. try {
  158. $ch = curl_init($uri);
  159. curl_setopt($ch, CURLOPT_HEADER, 0);
  160. curl_setopt($ch, CURLOPT_TIMEOUT, 4);
  161. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  162. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  163. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  164. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  165. $output = curl_exec($ch);
  166. } catch (Exception $e) {
  167. }
  168. return $output;
  169. }
  170. /**
  171. * Make a POST call to the bit.ly API.
  172. *
  173. * @param $uri
  174. * URI to call.
  175. * @param $fields
  176. * Array of fields to send.
  177. */
  178. function bitly_post_curl($uri, $fields, $header_array = array()) {
  179. $output = "";
  180. $fields_string = "";
  181. foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
  182. rtrim($fields_string,'&');
  183. try {
  184. $ch = curl_init($uri);
  185. if(is_array($header_array) && !empty($header_array)){
  186. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
  187. }
  188. curl_setopt($ch, CURLOPT_HEADER, 0);
  189. curl_setopt($ch,CURLOPT_POST,count($fields));
  190. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  191. curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  192. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  193. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  194. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  195. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  196. $output = curl_exec($ch);
  197. } catch (Exception $e) {
  198. }
  199. return $output;
  200. }
  201. ?>