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

/bitly.php

https://github.com/JoshuaEstes/BitlyPHP
PHP | 817 lines | 410 code | 28 blank | 379 comment | 34 complexity | 8367dd5de01da4e879dc266b5e5ab8ed 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. * @author Robin Monks <devlinks@gmail.com>
  11. */
  12. /**
  13. * The bitlyKey assigned to your bit.ly account. (http://bit.ly/a/account)
  14. */
  15. define('bitlyKey', 'YOUR_BITLY_ASSIGNED_KEY');
  16. /**
  17. * The bitlyLogin assigned to your bit.ly account. (http://bit.ly/a/account)
  18. */
  19. define('bitlyLogin' , 'YOUR_BITLY_LOGIN');
  20. /**
  21. * The client_id assigned to your OAuth app. (http://bit.ly/a/account)
  22. */
  23. define('bitly_clientid' , 'YOUR_BITLY_ASSIGNED_CLIENT_ID_FOR_OAUTH');
  24. /**
  25. * The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
  26. */
  27. define('bitly_secret' , 'YOUR_BITLY_ASSIGNED_CLIENT_SECRET_FOR_OAUTH');
  28. /**
  29. * The URI of the standard bitly v3 API.
  30. */
  31. define('bitly_api', 'http://api.bit.ly/v3/');
  32. /**
  33. * The URI of the bitly OAuth endpoints.
  34. */
  35. define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');
  36. /**
  37. * The URI for OAuth access token requests.
  38. */
  39. define('bitly_oauth_access_token', 'https://api-ssl.bit.ly/oauth/');
  40. /**
  41. * Given a longUrl, get the bit.ly shortened version.
  42. *
  43. * Example usage:
  44. * @code
  45. * $results = bitly_v3_shorten('http://knowabout.it', 'j.mp');
  46. * @endcode
  47. *
  48. * @param $longUrl
  49. * Long URL to be shortened.
  50. * @param $domain
  51. * Uses bit.ly (default), j.mp, or a bit.ly pro domain.
  52. * @param $x_login
  53. * User's login name.
  54. * @param $x_api_key
  55. * User's API key.
  56. *
  57. * @return
  58. * An associative array containing:
  59. * - url: The unique shortened link that should be used, this is a unique
  60. * value for the given bit.ly account.
  61. * - hash: A bit.ly identifier for long_url which is unique to the given
  62. * account.
  63. * - global_hash: A bit.ly identifier for long_url which can be used to track
  64. * aggregate stats across all matching bit.ly links.
  65. * - long_url: An echo back of the longUrl request parameter.
  66. * - new_hash: Will be set to 1 if this is the first time this long_url was
  67. * shortened by this user. It will also then be added to the user history.
  68. *
  69. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/shorten
  70. */
  71. function bitly_v3_shorten($longUrl, $domain = '', $x_login = '', $x_apiKey = '') {
  72. $result = array();
  73. $url = bitly_api . "shorten?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&longUrl=" . urlencode($longUrl);
  74. if ($domain != '') {
  75. $url .= "&domain=" . $domain;
  76. }
  77. if ($x_login != '' && $x_apiKey != '') {
  78. $url .= "&x_login=" . $x_login . "&x_apiKey=" . $x_apiKey;
  79. }
  80. $output = json_decode(bitly_get_curl($url));
  81. if (isset($output->{'data'}->{'hash'})) {
  82. $result['url'] = $output->{'data'}->{'url'};
  83. $result['hash'] = $output->{'data'}->{'hash'};
  84. $result['global_hash'] = $output->{'data'}->{'global_hash'};
  85. $result['long_url'] = $output->{'data'}->{'long_url'};
  86. $result['new_hash'] = $output->{'data'}->{'new_hash'};
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Expand a bit.ly url or hash.
  92. *
  93. * @param $data
  94. * Either a full bit.ly short url or a bit.ly hash to be expanded.
  95. *
  96. * @return
  97. * An associative array containing:
  98. * - hash: A bit.ly identifier for long_url which is unique to the given
  99. * account.
  100. * - long_url: The URL that the requested short_url or hash points to.
  101. * - user_hash: The corresponding bit.ly user identifier.
  102. * - global_hash: A bit.ly identifier for long_url which can be used to track
  103. * aggregate stats across all matching bit.ly links.
  104. *
  105. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/expand
  106. */
  107. function bitly_v3_expand($data) {
  108. $results = array();
  109. if (is_array($data)) {
  110. // we need to flatten this into one proper command
  111. $recs = array();
  112. foreach ($data as $rec) {
  113. $tmp = explode('/', $rec);
  114. $tmp = array_reverse($tmp);
  115. array_push($recs, $tmp[0]);
  116. }
  117. $data = implode('&hash=', $recs);
  118. } else {
  119. $tmp = explode('/', $data);
  120. $tmp = array_reverse($tmp);
  121. $data = $tmp[0];
  122. }
  123. // make the call to expand
  124. $url = bitly_api . "expand?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  125. $output = json_decode(bitly_get_curl($url));
  126. if (isset($output->{'data'}->{'expand'})) {
  127. foreach ($output->{'data'}->{'expand'} as $tmp) {
  128. $rec = array();
  129. $rec['hash'] = $tmp->{'hash'};
  130. $rec['long_url'] = $tmp->{'long_url'};
  131. $rec['user_hash'] = $tmp->{'user_hash'};
  132. $rec['global_hash'] = $tmp->{'global_hash'};
  133. array_push($results, $rec);
  134. }
  135. }
  136. return $results;
  137. }
  138. /**
  139. * Validate that a bit.ly login/apiKey combination is valid.
  140. *
  141. * @param $x_login
  142. * The end users user's bit.ly login (for validation).
  143. * @param $x_apiKey
  144. * The end users bit.ly apiKey (for validation).
  145. *
  146. * @return
  147. * TRUE if the combination is valid.
  148. *
  149. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/validate
  150. */
  151. function bitly_v3_validate($x_login, $x_apiKey) {
  152. $result = 0;
  153. $url = bitly_api . "validate?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&x_login=" . $x_login . "&x_apiKey=" . $x_apiKey;
  154. $output = json_decode(bitly_get_curl($url));
  155. if (isset($output->{'data'}->{'valid'})) {
  156. $result = $output->{'data'}->{'valid'};
  157. }
  158. return (bool) $result;
  159. }
  160. /**
  161. * For one or more bit.ly URL's or hashes, returns statistics about the clicks
  162. * on that link.
  163. *
  164. * @param $data
  165. * Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
  166. * and/or hashes.
  167. *
  168. * @return
  169. * A multidimensional numbered associative array containing:
  170. * - short_url: The unique bit.ly hash.
  171. * - global_hash: A bit.ly identifier for long_url which can be used to track
  172. * aggregate stats across all matching bit.ly links.
  173. * - user_clicks: The total count of clicks to this user's bit.ly link.
  174. * - user_hash: The corresponding bit.ly user identifier.
  175. * - global_clicks: The total count of clicks to all bit.ly links that point
  176. * to the same same long url.
  177. *
  178. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks
  179. */
  180. function bitly_v3_clicks($data) {
  181. $results = array();
  182. if (is_array($data)) {
  183. // we need to flatten this into one proper command
  184. $recs = array();
  185. foreach ($data as $rec) {
  186. $tmp = explode('/', $rec);
  187. $tmp = array_reverse($tmp);
  188. array_push($recs, $tmp[0]);
  189. }
  190. $data = implode('&hash=', $recs);
  191. } else {
  192. $tmp = explode('/', $data);
  193. $tmp = array_reverse($tmp);
  194. $data = $tmp[0];
  195. }
  196. $url = bitly_api . "clicks?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  197. $output = json_decode(bitly_get_curl($url));
  198. if (isset($output->{'data'}->{'clicks'})) {
  199. foreach ($output->{'data'}->{'clicks'} as $tmp) {
  200. $rec = array();
  201. $rec['short_url'] = $tmp->{'short_url'};
  202. $rec['global_hash'] = $tmp->{'global_hash'};
  203. $rec['user_clicks'] = $tmp->{'user_clicks'};
  204. $rec['user_hash'] = $tmp->{'user_hash'};
  205. $rec['global_clicks'] = $tmp->{'global_clicks'};
  206. array_push($results, $rec);
  207. }
  208. }
  209. return $results;
  210. }
  211. /**
  212. * Provides a list of referring sites for a specified bit.ly short link or hash,
  213. * and the number of clicks per referrer.
  214. *
  215. * @param $data
  216. * A bit.ly shortened URL or bit.ly hash.
  217. *
  218. * @return
  219. * An associative array containing:
  220. * - created_by: The service that created the link.
  221. * - global_hash: A bit.ly identifier for long_url which can be used to track
  222. * aggregate stats across all matching bit.ly links.
  223. * - short_url: The unique bit.ly hash.
  224. * - user_hash: The corresponding bit.ly user identifier.
  225. * - referrers: A multidimensional numbered associative array containing:
  226. * - clicks: Number of clicks from this referrer.
  227. * - referrer: (optional) Referring site.
  228. * - referrer_app: (optional) Referring application (e.g.: Tweetdeck).
  229. * - url: (optional) URL of referring application
  230. *
  231. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/referrers
  232. */
  233. function bitly_v3_referrers($data) {
  234. $results = array();
  235. $tmp = explode('/', $data);
  236. $tmp = array_reverse($tmp);
  237. $data = $tmp[0];
  238. $url = bitly_api . "referrers?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  239. $output = json_decode(bitly_get_curl($url));
  240. if (isset($output->{'data'}->{'referrers'})) {
  241. $results['created_by'] = $output->{'data'}->{'created_by'};
  242. $results['global_hash'] = $output->{'data'}->{'global_hash'};
  243. $results['short_url'] = $output->{'data'}->{'short_url'};
  244. $results['user_hash'] = $output->{'data'}->{'user_hash'};
  245. $results['referrers'] = array();
  246. foreach ($output->{'data'}->{'referrers'} as $tmp) {
  247. $rec = array();
  248. $rec['clicks'] = $tmp->{'clicks'};
  249. $rec['referrer'] = $tmp->{'referrer'};
  250. $rec['referrer_app'] = $tmp->{'referrer_app'};
  251. $rec['url'] = $tmp->{'url'};
  252. array_push($results['referrers'], $rec);
  253. }
  254. }
  255. return $results;
  256. }
  257. /**
  258. * Provides a list of countries from which clicks on a specified bit.ly short
  259. * link or hash have originated, and the number of clicks per country.
  260. *
  261. * @param $data
  262. * A bit.ly shortened URL or bit.ly hash.
  263. *
  264. * @return
  265. * An associative array containing:
  266. * - created_by: The service that created the link.
  267. * - global_hash: A bit.ly identifier for long_url which can be used to track
  268. * aggregate stats across all matching bit.ly links.
  269. * - short_url: The unique bit.ly hash.
  270. * - user_hash: The corresponding bit.ly user identifier.
  271. * - countries: A multidimensional numbered associative array containing:
  272. * - clicks: Number of clicks from this country.
  273. * - country: The country code these clicks originated from or null when
  274. * displaying clicks that could not be mapped to a specific country.
  275. *
  276. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/countries
  277. */
  278. function bitly_v3_countries($data) {
  279. $results = array();
  280. $tmp = explode('/', $data);
  281. $tmp = array_reverse($tmp);
  282. $data = $tmp[0];
  283. $url = bitly_api . "countries?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  284. $output = json_decode(bitly_get_curl($url));
  285. if (isset($output->{'data'}->{'countries'})) {
  286. $results['created_by'] = $output->{'data'}->{'created_by'};
  287. $results['global_hash'] = $output->{'data'}->{'global_hash'};
  288. $results['short_url'] = $output->{'data'}->{'short_url'};
  289. $results['user_hash'] = $output->{'data'}->{'user_hash'};
  290. $results['countries'] = array();
  291. foreach ($output->{'data'}->{'countries'} as $tmp) {
  292. $rec = array();
  293. $rec['clicks'] = $tmp->{'clicks'};
  294. $rec['country'] = $tmp->{'country'};
  295. array_push($results['countries'], $rec);
  296. }
  297. }
  298. return $results;
  299. }
  300. /**
  301. * For one or more bit.ly short urls or hashes, provides time series clicks per
  302. * minute for the last hour in reverse chronological order (most recent to least
  303. * recent).
  304. *
  305. * @param $data
  306. * Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
  307. * and/or hashes.
  308. *
  309. * @return
  310. * A multidimensional numbered associative array containing:
  311. * - clicks: An array with sixty entires, each for the number of clicks
  312. * received for the given link that minute.
  313. * - global_hash: A bit.ly identifier for long_url which can be used to track
  314. * aggregate stats across all matching bit.ly links.
  315. * - short_url: The unique bit.ly hash.
  316. * - user_hash: The corresponding bit.ly user identifier.
  317. *
  318. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks_by_minute
  319. */
  320. function bitly_v3_clicks_by_minute($data) {
  321. $results = array();
  322. if (is_array($data)) {
  323. // we need to flatten this into one proper command
  324. $recs = array();
  325. foreach ($data as $rec) {
  326. $tmp = explode('/', $rec);
  327. $tmp = array_reverse($tmp);
  328. array_push($recs, $tmp[0]);
  329. }
  330. $data = implode('&hash=', $recs);
  331. } else {
  332. $tmp = explode('/', $data);
  333. $tmp = array_reverse($tmp);
  334. $data = $tmp[0];
  335. }
  336. $url = bitly_api . "clicks_by_minute?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  337. $output = json_decode(bitly_get_curl($url));
  338. if (isset($output->{'data'}->{'clicks_by_minute'})) {
  339. foreach ($output->{'data'}->{'clicks_by_minute'} as $tmp) {
  340. $rec = array();
  341. $rec['clicks'] = $tmp->{'clicks'};
  342. $rec['global_hash'] = $tmp->{'global_hash'};
  343. $rec['short_url'] = $tmp->{'short_url'};
  344. $rec['user_hash'] = $tmp->{'user_hash'};
  345. array_push($results, $rec);
  346. }
  347. }
  348. return $results;
  349. }
  350. /**
  351. * For one or more bit.ly short urls or hashes, provides time series clicks per
  352. * day for the last 30 days in reverse chronological order (most recent to least
  353. * recent).
  354. *
  355. * @param $data
  356. * Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
  357. * and/or hashes.
  358. *
  359. * @return
  360. * A multidimensional numbered associative array containing:
  361. * - global_hash: A bit.ly identifier for long_url which can be used to track
  362. * aggregate stats across all matching bit.ly links.
  363. * - short_url: The unique bit.ly hash.
  364. * - user_hash: The corresponding bit.ly user identifier.
  365. * - clicks: A multidimensional numbered associative array containing:
  366. * - clicks: The number of clicks received for a given link that day.
  367. * - day_start: A time code representing the start of the day for which
  368. * click data is provided.
  369. *
  370. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks_by_day
  371. */
  372. function bitly_v3_clicks_by_day($data, $days = 7) {
  373. $results = array();
  374. if (is_array($data)) {
  375. // we need to flatten this into one proper command
  376. $recs = array();
  377. foreach ($data as $rec) {
  378. $tmp = explode('/', $rec);
  379. $tmp = array_reverse($tmp);
  380. array_push($recs, $tmp[0]);
  381. }
  382. $data = implode('&hash=', $recs);
  383. } else {
  384. $tmp = explode('/', $data);
  385. $tmp = array_reverse($tmp);
  386. $data = $tmp[0];
  387. }
  388. $url = bitly_api . "clicks_by_day?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&days=" . $days . "&hash=" . $data;
  389. $output = json_decode(bitly_get_curl($url));
  390. if (isset($output->{'data'}->{'clicks_by_day'})) {
  391. foreach ($output->{'data'}->{'clicks_by_day'} as $tmp) {
  392. $rec = array();
  393. $rec['global_hash'] = $tmp->{'global_hash'};
  394. $rec['short_url'] = $tmp->{'short_url'};
  395. $rec['user_hash'] = $tmp->{'user_hash'};
  396. $rec['clicks'] = array();
  397. $clicks = $tmp->{'clicks'};
  398. foreach ($clicks as $click) {
  399. $clickrec = array();
  400. $clickrec['clicks'] = $click->{'clicks'};
  401. $clickrec['day_start'] = $click->{'day_start'};
  402. array_push($rec['clicks'], $clickrec);
  403. }
  404. array_push($results, $rec);
  405. }
  406. }
  407. return $results;
  408. }
  409. /**
  410. * This is used to query whether a given short domain is assigned for bitly.Pro,
  411. * and is consequently a valid shortUrl parameter for other API calls.
  412. *
  413. * @param $domain
  414. * The short domain to check.
  415. *
  416. * @return
  417. * An associative array containing:
  418. * - domain: An echo back of the request parameter.
  419. * - bitly_pro_domain: 0 or 1 designating whether this is a current bitly.Pro
  420. * domain.
  421. *
  422. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/bitly_pro_domain
  423. */
  424. function bitly_v3_bitly_pro_domain($domain) {
  425. $result = array();
  426. $url = bitly_api . "bitly_pro_domain?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&domain=" . $domain;
  427. $output = json_decode(bitly_get_curl($url));
  428. if (isset($output->{'data'}->{'bitly_pro_domain'})) {
  429. $result['domain'] = $output->{'data'}->{'domain'};
  430. $result['bitly_pro_domain'] = $output->{'data'}->{'bitly_pro_domain'};
  431. }
  432. return $result;
  433. }
  434. /**
  435. * This is used to query for a bit.ly link based on a long URL.
  436. *
  437. * @param $data
  438. * One or more long URLs to lookup.
  439. *
  440. * @return
  441. * An associative array containing:
  442. * - global_hash: A bit.ly identifier for long_url which can be used to track
  443. * aggregate stats across all matching bit.ly links.
  444. * - short_url: The unique shortened link that should be used, this is a
  445. * unique value for the given bit.ly account.
  446. * - url: An echo back of the url parameter.
  447. *
  448. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/lookup
  449. */
  450. function bitly_v3_lookup($data) {
  451. $results = array();
  452. if (is_array($data)) {
  453. // we need to flatten this into one proper command
  454. $recs = array();
  455. foreach ($data as $rec) {
  456. array_push($recs, urlencode($rec));
  457. }
  458. $data = implode('&url=', $recs);
  459. } else {
  460. $data = urlencode($data);
  461. }
  462. $url = bitly_api . "lookup?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&url=" . $data;
  463. $output = json_decode(bitly_get_curl($url));
  464. if (isset($output->{'data'}->{'lookup'})) {
  465. foreach ($output->{'data'}->{'lookup'} as $tmp) {
  466. $rec = array();
  467. $rec['global_hash'] = $tmp->{'global_hash'};
  468. $rec['short_url'] = $tmp->{'short_url'};
  469. $rec['url'] = $tmp->{'url'};
  470. array_push($results, $rec);
  471. }
  472. }
  473. return $results;
  474. }
  475. /**
  476. * This is used by applications to lookup a bit.ly API key for a user given a
  477. * bit.ly username and password.
  478. *
  479. * @param $x_login
  480. * Bit.ly username.
  481. * @param $x_password
  482. * Bit.ly password.
  483. *
  484. * @return
  485. * An associative array containing:
  486. * - successful: An indicator of weather or not the login and password
  487. * combination is valid.
  488. * - username: The corresponding bit.ly users username.
  489. * - api_key: The corresponding bit.ly users API key.
  490. *
  491. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/authenticate
  492. */
  493. function bitly_v3_authenticate($x_login, $x_password) {
  494. $result = array();
  495. $url = bitly_api . "authenticate";
  496. $params = array();
  497. $params['login'] = bitlyLogin;
  498. $params['apiKey'] = bitlyKey;
  499. $params['format'] = "json";
  500. $params['x_login'] = $x_login;
  501. $params['x_password'] = $x_password;
  502. $output = json_decode(bitly_post_curl($url, $params));
  503. if (isset($output->{'data'}->{'authenticate'})) {
  504. $result['successful'] = $output->{'data'}->{'authenticate'}->{'successful'};
  505. $result['username'] = $output->{'data'}->{'authenticate'}->{'username'};
  506. $result['api_key'] = $output->{'data'}->{'authenticate'}->{'api_key'};
  507. }
  508. return $result;
  509. }
  510. /**
  511. * This is used to return the page title for a given bit.ly link.
  512. *
  513. * @param $data
  514. * Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
  515. * and/or hashes.
  516. *
  517. * @return
  518. * A multidimensional numbered associative array containing:
  519. * - created_by: The service that created the link.
  520. * - global_hash: A bit.ly identifier for long_url which can be used to track
  521. * aggregate stats across all matching bit.ly links.
  522. * - hash: The unique bit.ly hash.
  523. * - title: The HTML page title for the destination page (when available).
  524. * - user_hash: The corresponding bit.ly user identifier.
  525. *
  526. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/info
  527. */
  528. function bitly_v3_info($data) {
  529. $results = array();
  530. if (is_array($data)) {
  531. // we need to flatten this into one proper command
  532. $recs = array();
  533. foreach ($data as $rec) {
  534. $tmp = explode('/', $rec);
  535. $tmp = array_reverse($tmp);
  536. array_push($recs, $tmp[0]);
  537. }
  538. $data = implode('&hash=', $recs);
  539. } else {
  540. $tmp = explode('/', $data);
  541. $tmp = array_reverse($tmp);
  542. $data = $tmp[0];
  543. }
  544. // make the call to expand
  545. $url = bitly_api . "info?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
  546. $output = json_decode(bitly_get_curl($url));
  547. if (isset($output->{'data'}->{'info'})) {
  548. foreach ($output->{'data'}->{'info'} as $tmp) {
  549. $rec = array();
  550. $rec['created_by'] = $tmp->{'created_by'};
  551. $rec['global_hash'] = $tmp->{'global_hash'};
  552. $rec['hash'] = $tmp->{'hash'};
  553. $rec['title'] = $tmp->{'title'};
  554. $rec['user_hash'] = $tmp->{'user_hash'};
  555. array_push($results, $rec);
  556. }
  557. }
  558. return $results;
  559. }
  560. /**
  561. * Returns an OAuth access token as well as API users for a given code.
  562. *
  563. * @param $code
  564. * The OAuth verification code acquired via OAuth’s web authentication
  565. * protocol.
  566. * @param $redirect
  567. * The page to which a user was redirected upon successfully authenticating.
  568. *
  569. * @return
  570. * An associative array containing:
  571. * - login: The corresponding bit.ly users username.
  572. * - api_key: The corresponding bit.ly users API key.
  573. * - access_token: The OAuth access token for specified user.
  574. *
  575. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
  576. */
  577. function bitly_oauth_access_token($code, $redirect) {
  578. $results = array();
  579. $url = bitly_oauth_access_token . "access_token";
  580. $params = array();
  581. $params['client_id'] = bitly_clientid;
  582. $params['client_secret'] = bitly_secret;
  583. $params['code'] = $code;
  584. $params['redirect_uri'] = $redirect;
  585. $output = bitly_post_curl($url, $params);
  586. $parts = explode('&', $output);
  587. foreach ($parts as $part) {
  588. $bits = explode('=', $part);
  589. $results[$bits[0]] = $bits[1];
  590. }
  591. return $results;
  592. }
  593. /**
  594. * Provides the total clicks per day on a user’s bit.ly links.
  595. *
  596. * @param $access_token
  597. * The OAuth access token for the user.
  598. * @param $days
  599. * An integer value for the number of days (counting backwards from the
  600. * current day) from which to retrieve data (min:1, max:30, default:7).
  601. *
  602. * @return
  603. * An associative array containing:
  604. * - days: An echo of the dupplied days parameter.
  605. * - total_clicks: The total number of clicks over the supplied period.
  606. * - clicks: A multidimensional numbered associative array containing:
  607. * - clicks: The number of clicks received for a given link that day.
  608. * - day_start: A time code representing the start of the day for which
  609. * click data is provided.
  610. *
  611. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
  612. */
  613. function bitly_v3_user_clicks($access_token, $days = 7) {
  614. // $results = bitly_v3_user_clicks('BITLY_SUPPLIED_ACCESS_TOKEN');
  615. $results = array();
  616. $url = bitly_oauth_api . "user/clicks?access_token=" . $access_token . "&days=" . $days;
  617. $output = json_decode(bitly_get_curl($url));
  618. if (isset($output->{'data'}->{'clicks'})) {
  619. $results['days'] = $output->{'data'}->{'days'};
  620. $results['total_clicks'] = $output->{'data'}->{'total_clicks'};
  621. $results['clicks'] = array();
  622. foreach ($output->{'data'}->{'clicks'} as $clicks) {
  623. $rec = array();
  624. $rec['clicks'] = $clicks->{'clicks'};
  625. $rec['day_start'] = $clicks->{'day_start'};
  626. array_push($results['clicks'], $rec);
  627. }
  628. }
  629. return $results;
  630. }
  631. /**
  632. * Provides a list of referring sites for a specified bit.ly user, and the
  633. * number of clicks per referrer.
  634. *
  635. * @param $access_token
  636. * The OAuth access token for the user.
  637. * @param $days
  638. * An integer value for the number of days (counting backwards from the
  639. * current day) from which to retrieve data (min:1, max:30, default:7).
  640. *
  641. * @return
  642. * An associative array containing:
  643. * - days: An echo of the dupplied days parameter.
  644. * - referrers: A multidimensional numbered associative array containing:
  645. * - clicks: Number of clicks from this referrer.
  646. * - referrer: (optional) Referring site.
  647. *
  648. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
  649. */
  650. function bitly_v3_user_referrers($access_token, $days = 7) {
  651. // $results = bitly_v3_user_referrers('BITLY_SUPPLIED_ACCESS_TOKEN');
  652. $results = array();
  653. $url = bitly_oauth_api . "user/referrers?access_token=" . $access_token . "&days=" . $days;
  654. $output = json_decode(bitly_get_curl($url));
  655. if (isset($output->{'data'}->{'referrers'})) {
  656. $results['days'] = $output->{'data'}->{'days'};
  657. $results['referrers'] = array();
  658. foreach ($output->{'data'}->{'referrers'} as $referrers) {
  659. $recs = array();
  660. foreach ($referrers as $ref) {
  661. $rec = array();
  662. $rec['referrer'] = $ref->{'referrer'};
  663. $rec['clicks'] = $ref->{'clicks'};
  664. array_push($recs, $rec);
  665. }
  666. array_push($results['referrers'], $recs);
  667. }
  668. }
  669. return $results;
  670. }
  671. /**
  672. * Provides a list of referring sites for a specified bit.ly short link or hash,
  673. * and the number of clicks per referrer.
  674. *
  675. * @param $access_token
  676. * The OAuth access token for the user.
  677. * @param $days
  678. * An integer value for the number of days (counting backwards from the
  679. * current day) from which to retrieve data (min:1, max:30, default:7).
  680. *
  681. * @return
  682. * An associative array containing:
  683. * - days: An echo of the dupplied days parameter.
  684. * - referrers: A multidimensional numbered associative array containing:
  685. * - clicks: Number of clicks from this referrer.
  686. * - countries: (optional) Country code for where the clicks originated.
  687. *
  688. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
  689. */
  690. function bitly_v3_user_countries($access_token, $days = 7) {
  691. // $results = bitly_v3_user_countries('BITLY_SUPPLIED_ACCESS_TOKEN');
  692. $results = array();
  693. $url = bitly_oauth_api . "user/countries?access_token=" . $access_token . "&days=" . $days;
  694. $output = json_decode(bitly_get_curl($url));
  695. if (isset($output->{'data'}->{'countries'})) {
  696. $results['days'] = $output->{'data'}->{'days'};
  697. $results['countries'] = array();
  698. foreach ($output->{'data'}->{'countries'} as $countries) {
  699. $recs = array();
  700. foreach ($countries as $country) {
  701. $rec = array();
  702. $rec['country'] = $country->{'country'};
  703. $rec['clicks'] = $country->{'clicks'};
  704. array_push($recs, $rec);
  705. }
  706. array_push($results['countries'], $recs);
  707. }
  708. }
  709. return $results;
  710. }
  711. /**
  712. * Provides a given user’s 100 most popular links based on click traffic in the
  713. * past hour, and the number of clicks per link.
  714. *
  715. * @param $access_token
  716. * The OAuth access token for the user.
  717. *
  718. * @return
  719. * A multidimensional numbered associative array containing:
  720. * - user_hash: The corresponding bit.ly user identifier.
  721. * - clicks: Number of clicks on this link.
  722. *
  723. * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
  724. */
  725. function bitly_v3_user_realtime_links($access_token) {
  726. // $results = bitly_v3_user_realtime_links('BITLY_SUPPLIED_ACCESS_TOKEN');
  727. $results = array();
  728. $url = bitly_oauth_api . "user/realtime_links?format=json&access_token=" . $access_token;
  729. $output = json_decode(bitly_get_curl($url));
  730. if (isset($output->{'data'}->{'realtime_links'})) {
  731. foreach ($output->{'data'}->{'realtime_links'} as $realtime_links) {
  732. $rec = array();
  733. $rec['clicks'] = $realtime_links->{'clicks'};
  734. $rec['user_hash'] = $realtime_links->{'user_hash'};
  735. array_push($results, $rec);
  736. }
  737. }
  738. return $results;
  739. }
  740. /**
  741. * Make a GET call to the bit.ly API.
  742. *
  743. * @param $uri
  744. * URI to call.
  745. */
  746. function bitly_get_curl($uri) {
  747. $output = "";
  748. try {
  749. $ch = curl_init($uri);
  750. curl_setopt($ch, CURLOPT_HEADER, 0);
  751. curl_setopt($ch, CURLOPT_TIMEOUT, 4);
  752. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  753. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  754. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  755. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  756. $output = curl_exec($ch);
  757. } catch (Exception $e) {
  758. }
  759. return $output;
  760. }
  761. /**
  762. * Make a POST call to the bit.ly API.
  763. *
  764. * @param $uri
  765. * URI to call.
  766. * @param $fields
  767. * Array of fields to send.
  768. */
  769. function bitly_post_curl($uri, $fields) {
  770. $output = "";
  771. $fields_string = "";
  772. foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  773. rtrim($fields_string,'&');
  774. try {
  775. $ch = curl_init($uri);
  776. curl_setopt($ch, CURLOPT_HEADER, 0);
  777. curl_setopt($ch,CURLOPT_POST,count($fields));
  778. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  779. curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  780. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  781. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  782. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  783. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  784. $output = curl_exec($ch);
  785. } catch (Exception $e) {
  786. }
  787. return $output;
  788. }
  789. ?>