GooglLib.php 3.0 KB

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