GooglLib.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @return void
  31. */
  32. public function __construct($apiKey = null) {
  33. if ($apiKey === null) {
  34. $apiKey = Configure::read('Googl.key');
  35. }
  36. if ($apiKey) {
  37. $this->APIKey = $apiKey;
  38. }
  39. }
  40. /**
  41. * Reverse the shortening process
  42. * TODO: rename to expand
  43. *
  44. * @param strin $url
  45. * @return result as array
  46. */
  47. public function getLong($shortURL, $projection = null) {
  48. $vars = '?shortUrl=' . $shortURL;
  49. if ($projection) {
  50. $vars .= '&projection=' . $projection;
  51. }
  52. if ($this->APIKey) {
  53. $vars .= '&key=' . $this->APIKey;
  54. }
  55. $ch = curl_init($this->API . $vars);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. $result = curl_exec($ch);
  59. curl_close($ch);
  60. $array = json_decode($result, true);
  61. return $array;
  62. }
  63. /**
  64. * Shorten a long url
  65. *
  66. * @param string $url
  67. * @return array result as array or false on failure
  68. */
  69. public function getShort($longURL) {
  70. $vars = '';
  71. if ($this->APIKey) {
  72. $vars .= "?key=$this->APIKey";
  73. }
  74. $ch = curl_init($this->API . $vars);
  75. curl_setopt($ch, CURLOPT_POST, 1);
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  77. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key": "'.$this->APIKey.'", "longUrl": "' . $longURL . '"}');
  80. $result = curl_exec($ch);
  81. curl_close($ch);
  82. $array = json_decode($result, true);
  83. if (empty($array['id'])) {
  84. # throw error?
  85. CakeLog::write('googl', $longURL.' - '.print_r($array, true));
  86. return false;
  87. }
  88. $separator = strrpos($array['id'], '/');
  89. $array['key'] = substr($array['id'], $separator+1);
  90. return $array;
  91. }
  92. /**
  93. * FIXME: not working yet
  94. * TODO: use oacurl etc
  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. * @param string $key
  111. * @return string url
  112. */
  113. public static function statisticsUrl($key) {
  114. $url = 'http://goo.gl/#analytics/goo.gl/'.$key.'/all_time';
  115. return $url;
  116. }
  117. }