1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Main Bit.ly PHP class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace sineverba\Bitly; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use sineverba\Bitly\Exceptions\SineverbaException; |
10
|
|
|
|
11
|
|
|
class Bitly |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var the token |
16
|
|
|
*/ |
17
|
|
|
private $token; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base API V4 URL |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
private $api_url = "https://api-ssl.bitly.com/v4/"; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Final short url |
27
|
|
|
* |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
* @author sineverba |
30
|
|
|
*/ |
31
|
|
|
private $short_url; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Bitly constructor. |
35
|
|
|
* We need the token from your personal page: |
36
|
|
|
* |
37
|
|
|
* https://bitly.com/a/oauth_apps |
38
|
|
|
* |
39
|
|
|
* @param $token |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
public function __construct($token=null) |
43
|
|
|
{ |
44
|
|
|
if ($token===null) { |
45
|
|
|
throw new SineverbaException("Token cannot be empty"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->setToken($token); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get shorten link. |
53
|
|
|
* @param $url original URL |
54
|
|
|
* |
55
|
|
|
* @author @sineverba |
56
|
|
|
* @since 1.0.0 |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
public function createShortLink($url=null) |
60
|
|
|
{ |
61
|
|
|
if ($url===null) { |
62
|
|
|
throw new SineverbaException("URL cannot be empty"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$option = array(); |
66
|
|
|
$option['base_uri'] = $this->getApiUrl(); |
67
|
|
|
$option['verify'] = false; |
68
|
|
|
$client = new \GuzzleHttp\Client($option); |
69
|
|
|
|
70
|
|
|
$headers = [ |
71
|
|
|
'Authorization' => 'Bearer ' . $this->getToken(), |
72
|
|
|
'Accept' => 'application/json', |
73
|
|
|
'Content-Type' => 'application/json' |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
$post = array(); |
77
|
|
|
$post['long_url'] = $url; |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
|
81
|
|
|
$response = $client->request('POST', 'shorten', [ |
82
|
|
|
'headers' => $headers, |
83
|
|
|
'json' => $post |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$response = json_decode($response->getBody()); |
87
|
|
|
|
88
|
|
|
if (is_object($response) && isset($response->link)) { |
89
|
|
|
$short_url = $response->link; |
|
|
|
|
90
|
|
|
$this->setShortUrl($short_url); |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
throw new SineverbaException("No short link found. Probably missing/wrong token"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
96
|
|
|
|
97
|
|
|
//echo $e->getMessage(); |
98
|
|
|
throw new SineverbaException("No short link found. Probably missing/wrong token"); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return null |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
private function getToken() |
108
|
|
|
{ |
109
|
|
|
return $this->token; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param null $token |
114
|
|
|
*/ |
115
|
|
|
private function setToken($token): void |
116
|
|
|
{ |
117
|
|
|
$this->token = $token; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the base API Url |
122
|
|
|
* |
123
|
|
|
* @since 1.0.0 |
124
|
|
|
* @author @sineverba |
125
|
|
|
*/ |
126
|
|
|
private function getApiUrl() |
127
|
|
|
{ |
128
|
|
|
return $this->api_url; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getShortUrl() |
135
|
|
|
{ |
136
|
|
|
return $this->short_url; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param mixed $short_url |
141
|
|
|
*/ |
142
|
|
|
private function setShortUrl($short_url): void |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$this->short_url = $short_url; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.