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