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