This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogl.class.php
94 lines (84 loc) · 2.12 KB
/
googl.class.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
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
<?php
/**
* Googl
*
* @author Eslam Mahmoud
* @url http://eslam.me/
* @copyright Creative Commons Attribution-ShareAlike 3.0 Unported License.
* @version 0.2
* @access public
*/
class Googl {
//application key
private $APIKey;
//api url
private $API = "https://www.googleapis.com/urlshortener/v1/url";
// short_url
public $short_url;
/**
* Googl::Googl()
*
* @param string $apiKey
* @return void
*/
function Googl($apiKey=""){
if ($apiKey != ""){
$this->APIKey = $apiKey;
}
}
/**
* Googl::get_long()
*
* @param url as string $shortURL
* @return result as array
*/
function get_long($shortURL , $analytics = false){
$url = $this->API.'?shortUrl='.$shortURL;
if ($this->APIKey){
$url .= '&key='.$this->APIKey;
}
if ($analytics){
$url .= '&projection=FULL';
}
$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;
}
/**
* Googl::set_short()
*
* @param url as string $longURL
* @return result as array
*/
function set_short($longURL){
$vars = "";
if ($this->APIKey){
$vars .= "?key=$this->APIKey";
}
$ch = curl_init($this->API.$vars);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"longUrl": "' . $longURL . '"}');
$result=curl_exec($ch);
curl_close($ch);
$array = json_decode($result, true);
$this->short_url = $array['id'];
return $array;
}
/**
* Googl::get_short()
*
* @param
* @return attribute short_url
*/
function get_short(){
return $this->short_url;
}
}
?>