-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleUrlShortner.php
45 lines (45 loc) · 1.63 KB
/
GoogleUrlShortner.php
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
<?php
class GoogleUrlShortner{
//get yout own key from google
public $key = "";
public $googleLink = "https://www.googleapis.com/urlshortener/v1/url";
private function sendReceive($data = null,$destinationLink){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $destinationLink);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if($data != null){
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$err = curl_error($ch);
// $response = curl_exec($ch);
curl_close($ch);
return json_decode($response,true);
}
public function shorten($url){
$link = "$this->googleLink?key=$this->key";
$this->UrlValidator($url);
$data = '{"longUrl": "'.$url.'"}';
echo $link;
return $this->sendReceive($data,$link);
}
public function expandUrl($shortUrl){
$link = "$this->googleLink?key=$this->key&shortUrl=$shortUrl";
return $this->sendReceive(null,$link);
}
public function analytics($url){
$link = "$this->googleLink?key=$this->key&shortUrl=$shortUrl&projection=FULL";
return $this->sendReceive(null,$link);
}
}
//example usage
$shortner = new GoogleUrlShortner();
$result = $shortner->shorten("http://kiranruth.com");
$shortUrl = $result['id'];
$result = $shortner->expandUrl($shortUrl);
$analytics = $shortner->analytics($shortUrl);
?>