| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- App::uses('CakeLog', 'Log');
- /**
- * Googl Url Shortener
- * @see http://goo.gl
- *
- * @author Eslam Mahmoud
- * @url http://hunikal.com/
- * @copyright Creative Commons Attribution-ShareAlike 3.0 Unported License.
- * @version 0.1
- *
- * TODO: implement OAuth
- *
- * @edited Mark Scherer
- */
- class GooglLib {
- const PROJECTION_FULL = 'FULL';
- const PROJECTION_CLICKS = 'ANALYTICS_CLICKS';
- const PROJECTION_TOP = 'ANALYTICS_TOP_STRINGS';
- /**
- * Application key
- */
- protected $apiKey;
- /**
- * API URL
- */
- protected $apiUrl = "https://www.googleapis.com/urlshortener/v1/url";
- /**
- * @param string $apiKey (optional)
- */
- public function __construct($apiKey = null) {
- if ($apiKey === null) {
- $apiKey = Configure::read('Googl.key');
- }
- if ($apiKey) {
- $this->apiKey = $apiKey;
- }
- }
- /**
- * Reverse the shortening process
- * TODO: rename to expand
- *
- * @param strin $url
- * @return result as array
- */
- public function getLong($shortURL, $projection = null) {
- $vars = '?shortUrl=' . $shortURL;
- if ($projection) {
- $vars .= '&projection=' . $projection;
- }
- if ($this->apiKey) {
- $vars .= '&key=' . $this->apiKey;
- }
- $ch = curl_init($this->apiUrl . $vars);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- $array = json_decode($result, true);
- return $array;
- }
- /**
- * Shorten a long url
- *
- * @param string $url
- * @return array result as array or false on failure
- */
- public function getShort($longURL) {
- $vars = '';
- if ($this->apiKey) {
- $vars .= "?key=$this->apiKey";
- }
- $ch = curl_init($this->apiUrl . $vars);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key": "' . $this->apiKey . '", "longUrl": "' . $longURL . '"}');
- $result = curl_exec($ch);
- curl_close($ch);
- $array = json_decode($result, true);
- if (empty($array['id'])) {
- // throw error?
- CakeLog::write('googl', $longURL . ' - ' . print_r($array, true));
- return false;
- }
- $separator = strrpos($array['id'], '/');
- $array['key'] = substr($array['id'], $separator + 1);
- return $array;
- }
- /**
- * FIXME: not working yet
- * TODO: use oacurl etc
- *
- * @return array
- */
- public function getHistory() {
- $vars = '';
- $url = $this->apiUrl . '/history';
- $ch = curl_init($url . $vars);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- $array = json_decode($result, true);
- return $array;
- }
- /**
- * Retrieve the statistics for this key.
- *
- * @param string $key
- * @return array
- */
- public function getStatistics($key) {
- $url = $this->apiUrl . '?shortUrl=http://goo.gl/' . $key . '&projection=FULL&key=' . $this->apiKey;
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- $array = json_decode($result, true);
- return $array;
- }
- /**
- * Retrieve the url for the statistics page for this key
- *
- * @param string $key
- * @return string url
- */
- public static function statisticsUrl($key) {
- $url = 'http://goo.gl/#analytics/goo.gl/' . $key . '/all_time';
- return $url;
- }
- }
|