-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoogleURL.php
More file actions
130 lines (117 loc) · 3.07 KB
/
GoogleURL.php
File metadata and controls
130 lines (117 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Google Shorner URL
*
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @author Sophy Prak, sophy.prak@gmail.com
* @link http://kooms.info
*/
/**
* Define google url shortener api
*/
define("API_URL", "https://www.googleapis.com/urlshortener/v1/url?key=");
/**
* GoogleURL to generate shorten and expend url
*
*
*/
class GoogleURL
{
/**
*
* @var string
*/
private $_apiUrl;
/**
* Short url
*
* @var string
*/
private $_shortUrl;
/**
*
* @param string $apiKey
*/
public function __construct($apiKey) {
//Google URL Shortener
$this->_apiUrl = API_URL.$apiKey;
}
/**
* Make shorten url
*
* @access public
* @param string $longUrl long url
* @return string shorten url
*/
public function shorten($longUrl)
{
$data = $this->getContent($longUrl);
$this->_shortUrl = $data->id;
return $data->id;
}
/**
* Expand short url
*
* @access public
* @param string $shortUrl
* @return string
*/
public function expand($shortUrl)
{
$data = $this->getContent($shortUrl, "expand");
$this->_shortUrl = $data->id;
return $data->longUrl;
}
/**
* Get number of long url click
*
* @access public
* @return int
*/
public function getLongUrlCliks(){
$data = $this->getContent($this->_shortUrl, 'info');
return $data->analytics->allTime->longUrlClicks;
}
/**
* Get number of short url click
*
* @access public
* @return int
*/
public function getShortUrlClicks(){
$data = $this->getContent($this->_shortUrl, 'info');
return $data->analytics->allTime->shortUrlClicks;
}
/**
* Get data from google api
*
* @param string $url
* @param string $requestType
* @return string json format
*/
private function getContent( $url, $requestType = "shorten" ){
$curl = curl_init();
if( $requestType === "expand" ){
curl_setopt($curl, CURLOPT_URL, $this->_apiUrl.'&shortUrl='.$url);
}else if( $requestType === "info" ){
curl_setopt($curl, CURLOPT_URL, $this->_apiUrl.'&shortUrl='.$url.'&projection=FULL');
}else{
curl_setopt($curl, CURLOPT_URL, $this->_apiUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $url)));
}
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
//change the response json string to object
return json_decode($response);
}
}
?>