GooglLib.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. App::uses('CakeLog', 'Log');
  3. /**
  4. * Googl Url Shortener
  5. * @see http://goo.gl
  6. *
  7. * @author Eslam Mahmoud
  8. * @url http://hunikal.com/
  9. * @copyright Creative Commons Attribution-ShareAlike 3.0 Unported License.
  10. * @version 0.1
  11. *
  12. * TODO: implement OAuth
  13. *
  14. * @edited Mark Scherer
  15. */
  16. class GooglLib {
  17. const PROJECTION_FULL = 'FULL';
  18. const PROJECTION_CLICKS = 'ANALYTICS_CLICKS';
  19. const PROJECTION_TOP = 'ANALYTICS_TOP_STRINGS';
  20. /**
  21. * Application key
  22. */
  23. protected $APIKey;
  24. /**
  25. * Api url
  26. */
  27. protected $API = "https://www.googleapis.com/urlshortener/v1/url";
  28. /**
  29. * @param string $apiKey (optional)
  30. */
  31. public function __construct($apiKey = null) {
  32. if ($apiKey === null) {
  33. $apiKey = Configure::read('Googl.key');
  34. }
  35. if ($apiKey) {
  36. $this->APIKey = $apiKey;
  37. }
  38. }
  39. /**
  40. * Reverse the shortening process
  41. * TODO: rename to expand
  42. *
  43. * @param strin $url
  44. * @return result as array
  45. */
  46. public function getLong($shortURL, $projection = null) {
  47. $vars = '?shortUrl=' . $shortURL;
  48. if ($projection) {
  49. $vars .= '&projection=' . $projection;
  50. }
  51. if ($this->APIKey) {
  52. $vars .= '&key=' . $this->APIKey;
  53. }
  54. $ch = curl_init($this->API . $vars);
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  57. $result = curl_exec($ch);
  58. curl_close($ch);
  59. $array = json_decode($result, true);
  60. return $array;
  61. }
  62. /**
  63. * Shorten a long url
  64. *
  65. * @param string $url
  66. * @return array result as array or false on failure
  67. */
  68. public function getShort($longURL) {
  69. $vars = '';
  70. if ($this->APIKey) {
  71. $vars .= "?key=$this->APIKey";
  72. }
  73. $ch = curl_init($this->API . $vars);
  74. curl_setopt($ch, CURLOPT_POST, 1);
  75. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key": "' . $this->APIKey . '", "longUrl": "' . $longURL . '"}');
  79. $result = curl_exec($ch);
  80. curl_close($ch);
  81. $array = json_decode($result, true);
  82. if (empty($array['id'])) {
  83. # throw error?
  84. CakeLog::write('googl', $longURL . ' - ' . print_r($array, true));
  85. return false;
  86. }
  87. $separator = strrpos($array['id'], '/');
  88. $array['key'] = substr($array['id'], $separator + 1);
  89. return $array;
  90. }
  91. /**
  92. * FIXME: not working yet
  93. * TODO: use oacurl etc
  94. *
  95. * @return array
  96. */
  97. public function getHistory() {
  98. $vars = '';
  99. $url = $this->API . '/history';
  100. $ch = curl_init($url . $vars);
  101. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  103. $result = curl_exec($ch);
  104. curl_close($ch);
  105. $array = json_decode($result, true);
  106. return $array;
  107. }
  108. /**
  109. * Retrieve the url for the statistics page for this key
  110. *
  111. * @param string $key
  112. * @return string url
  113. */
  114. public static function statisticsUrl($key) {
  115. $url = 'http://goo.gl/#analytics/goo.gl/' . $key . '/all_time';
  116. return $url;
  117. }
  118. }