1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LiveStream; |
4
|
|
|
|
5
|
|
|
use CURLFile; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
use LiveStream\Resources\Event; |
9
|
|
|
use LiveStream\Resources\RTMPKey; |
10
|
|
|
use LiveStream\Resources\Account; |
11
|
|
|
use LiveStream\Interfaces\Resource; |
12
|
|
|
|
13
|
|
|
use LiveStream\Exceptions\LiveStreamException; |
14
|
|
|
use LiveStream\Exceptions\InValidResourceException; |
15
|
|
|
|
16
|
|
|
class LiveStream |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Live Stream API Base URL. |
20
|
|
|
*/ |
21
|
|
|
const BASE_URL = 'https://livestreamapis.com/v3/'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* LiveStream API HTTP Codes. |
25
|
|
|
*/ |
26
|
|
|
const ERROR_CODES = [ |
27
|
|
|
400 => 'Bad Request: Your request is not properly constructed.', |
28
|
|
|
401 => 'Unauthorized: Your API key is incorrect.', |
29
|
|
|
403 => 'Forbidden:', |
30
|
|
|
405 => 'Method Not Allowed: You tried to access a resource with an invalid method.', |
31
|
|
|
406 => 'Not Acceptable: You requested a format that is not JSON.', |
32
|
|
|
500 => 'Internal Server Error: We had a problem with our server. Please try again later.', |
33
|
|
|
503 => 'Service Unavailable: We are temporarily offline for maintanance. Please try again later.' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* API_KEY. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $apiKey; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class Constructor |
45
|
|
|
* |
46
|
|
|
* @param string $apiKey |
47
|
|
|
*/ |
48
|
|
|
public function __construct(string $apiKey) |
49
|
|
|
{ |
50
|
|
|
$this->apiKey = $apiKey; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get Linked LiveStream Accounts |
55
|
|
|
* |
56
|
|
|
* @return array Array of LiveStream Accounts |
57
|
|
|
*/ |
58
|
|
|
public function getAccounts(): array |
59
|
|
|
{ |
60
|
|
|
$response = $this->request('accounts'); |
61
|
|
|
|
62
|
|
|
$accounts = []; |
63
|
|
|
|
64
|
|
|
foreach (json_decode($response) as $account) { |
65
|
|
|
$accounts[] = Account::fromObject($account); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $accounts; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Specific Account |
73
|
|
|
* |
74
|
|
|
* @param integer $accountId |
75
|
|
|
* @return Account|null |
76
|
|
|
*/ |
77
|
|
|
public function getAccount(int $accountId): ?Account |
78
|
|
|
{ |
79
|
|
|
$response = $this->request("accounts/$accountId"); |
80
|
|
|
if ($response === null) return null; |
81
|
|
|
|
82
|
|
|
return Account::fromObject(json_decode($response)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create New Event |
87
|
|
|
* |
88
|
|
|
* @param integer $accountId |
89
|
|
|
* @param Event $event |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function createEvent(int $accountId, Event &$event): bool |
93
|
|
|
{ |
94
|
|
|
$event->validate(); |
95
|
|
|
|
96
|
|
|
$response = $this->request("accounts/$accountId/events", 'post', $event); |
97
|
|
|
|
98
|
|
|
if ($response === null) return false; |
99
|
|
|
|
100
|
|
|
$event = Event::fromObject(json_decode($response)); |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Update Event. |
107
|
|
|
* |
108
|
|
|
* @param integer $accountId |
109
|
|
|
* @param LiveStream\Resources\Event $event |
|
|
|
|
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function updateEvent(int $accountId, Event $event): bool |
113
|
|
|
{ |
114
|
|
|
$event->validate(true); |
115
|
|
|
|
116
|
|
|
$response = $this->request("accounts/$accountId/events/$event->id", 'put', $event); |
|
|
|
|
117
|
|
|
|
118
|
|
|
if ($response === null) return false; |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Undocumented function |
125
|
|
|
* |
126
|
|
|
* @param integer $accountId |
127
|
|
|
* @param integer $eventId |
128
|
|
|
* @param string $filePath |
129
|
|
|
* @return boolean |
130
|
|
|
*/ |
131
|
|
|
public function updateEventLogo(int $accountId, int $eventId, string $filePath): Event |
132
|
|
|
{ |
133
|
|
|
if (!in_array(mime_content_type($filePath), [ |
134
|
|
|
'image/png', |
135
|
|
|
'image/jpg', |
136
|
|
|
'image/jpeg', |
137
|
|
|
'image/gif' |
138
|
|
|
])) { |
139
|
|
|
throw new InValidResourceException('Logo', 'poster (MIME Type must be one of image/png, image/jpg, image/gif)'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$response = $this->request("accounts/$accountId/events/$eventId/logo", 'put', null, null, [ |
143
|
|
|
'logo' => new CURLFile($filePath, mime_content_type($filePath), basename($filePath)) |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
if ($response == null) return null; |
|
|
|
|
147
|
|
|
|
148
|
|
|
return Event::fromObject(json_decode($response)); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get Draft Events |
153
|
|
|
* |
154
|
|
|
* @param integer $accountId |
155
|
|
|
* @param integer $page |
156
|
|
|
* @param integer $maxItems |
157
|
|
|
* @param string $order |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public function getDraftEvents( |
162
|
|
|
int $accountId, |
163
|
|
|
int $page = 1, |
164
|
|
|
int $maxItems = 10, |
165
|
|
|
string $order = 'desc' |
166
|
|
|
): array { |
167
|
|
|
$response = $this->request("accounts/$accountId/draft_events", 'get', null, [ |
168
|
|
|
'page' => $page, |
169
|
|
|
'maxItems' => $maxItems, |
170
|
|
|
'order' => $order |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
if ($response === null) return null; |
|
|
|
|
174
|
|
|
|
175
|
|
|
$events = []; |
176
|
|
|
|
177
|
|
|
foreach (json_decode($response)->data as $event) { |
178
|
|
|
$events[] = Event::fromObject($event); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $events; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get RTMP Key. |
186
|
|
|
* |
187
|
|
|
* @param integer $accountId |
188
|
|
|
* @param integer $eventId |
189
|
|
|
* |
190
|
|
|
* @return \LiveStream\Resources\RTMPKey|null |
191
|
|
|
*/ |
192
|
|
|
public function getRtmpKey( |
193
|
|
|
int $accountId, |
194
|
|
|
int $eventId, |
195
|
|
|
bool $notifyFollowers = false, |
196
|
|
|
bool $publishVideo = false, |
197
|
|
|
bool $saveVideo = false |
198
|
|
|
): ?RTMPKey { |
199
|
|
|
|
200
|
|
|
$response = $this->request("accounts/$accountId/events/$eventId/rtmp", 'get', null, [ |
201
|
|
|
'notifyFollowers' => $notifyFollowers, |
202
|
|
|
'publishVideo' => $publishVideo, |
203
|
|
|
'saveVideo' => $saveVideo |
204
|
|
|
]); |
205
|
|
|
|
206
|
|
|
if ($response === null) return null; |
207
|
|
|
|
208
|
|
|
return RTMPKey::fromObject(json_decode($response)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Reset RTMPKey |
213
|
|
|
* |
214
|
|
|
* @param integer $accountId |
215
|
|
|
* @param integer $eventId |
216
|
|
|
* @return \LiveStream\Resources\RTMPKey|null |
217
|
|
|
*/ |
218
|
|
|
public function resetRtmpKey(int $accountId, int $eventId): ?RTMPKey |
219
|
|
|
{ |
220
|
|
|
$response = $this->request("accounts/$accountId/events/$eventId/rtmp", 'put'); |
221
|
|
|
|
222
|
|
|
if ($response === null) return null; |
223
|
|
|
|
224
|
|
|
return RTMPKey::fromObject(json_decode($response)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* CURL Request |
229
|
|
|
* |
230
|
|
|
* @param string $endpoint |
231
|
|
|
* @return |
232
|
|
|
*/ |
233
|
|
|
private function request( |
234
|
|
|
string $endpoint, |
235
|
|
|
string $verb = 'get', |
236
|
|
|
?Resource $body = null, |
237
|
|
|
?array $query = null, |
238
|
|
|
?array $multipartFormData = null |
239
|
|
|
): ?string { |
240
|
|
|
$ch = curl_init(); |
241
|
|
|
|
242
|
|
|
if (!$ch) throw new Exception("Could not initialize CURL."); |
|
|
|
|
243
|
|
|
|
244
|
|
|
curl_setopt( |
245
|
|
|
$ch, |
246
|
|
|
CURLOPT_URL, |
247
|
|
|
$this->get_base_url() . $endpoint . ($query ? '?' . http_build_query($query) : '') |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey . ':'); |
251
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
252
|
|
|
|
253
|
|
|
if ($verb != 'get') { |
254
|
|
|
if ($verb == 'post') curl_setopt($ch, CURLOPT_POST, true); |
255
|
|
|
if ($verb == 'put') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
256
|
|
|
if ($body) { |
257
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
258
|
|
|
'Content-Type: ' . $body->getContentType() |
259
|
|
|
]); |
260
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body->getResourceBody()); |
261
|
|
|
} elseif ($multipartFormData) { |
262
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
263
|
|
|
'Content-Type: multipart/form-data' |
264
|
|
|
]); |
265
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipartFormData); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$response = curl_exec($ch); |
270
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
271
|
|
|
|
272
|
|
|
curl_close($ch); |
273
|
|
|
|
274
|
|
|
if ($code == 200 || $code == 201) return $response; |
275
|
|
|
|
276
|
|
|
if ($code == 404) return null; |
277
|
|
|
|
278
|
|
|
if ($code <= 199) throw new Exception("A CURL erorr with code '$code', has occurred."); |
279
|
|
|
|
280
|
|
|
if ($code == 403) throw new LiveStreamException(self::ERROR_CODES[$code] . ' ' . json_decode($response)->message); |
281
|
|
|
|
282
|
|
|
throw new LiveStreamException(self::ERROR_CODES[$code]); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get Base URL. |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
private function get_base_url(): string |
291
|
|
|
{ |
292
|
|
|
return getenv('LIB_ENV') == 'testing' ? 'http://127.0.0.1:3067/' : self::BASE_URL; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|