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