-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbunny-stream.php
343 lines (314 loc) · 11.7 KB
/
bunny-stream.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
class BunnyCDNStream
{
// Base URL for the API
const baseUrl = "http://video.bunnycdn.com/library/";
// Stream Library ID
public $streamLibraryId = "";
// API key
private $apiAccessKey = "";
public function __construct($streamLibraryId, $apiAccessKey)
{
$this->streamLibraryId = $streamLibraryId;
$this->apiAccessKey = $apiAccessKey;
}
/**
* Generate base URL containing the library ID.
*
* @param string $endpoint The endpoint to append to the URL.
* @return string Request URL
*/
private function generateBaseUrl($endpoint)
{
return BunnyCDNStream::baseUrl . $this->streamLibraryId . $endpoint;
}
/**
* Send cURL request to the API
*
* @param string $url Endpoint to send request to
* @param string $reqType The request type (GET, POST, PUT, DELETE)
* @param string $contentType The content type of the request (application/json, application/x-www-form-urlencoded, multipart/form-data, etc.)
* @param array $payload The payload to send (request body)
* @param array $curl_opts Additional cURL options (array of curl_setopt() options as keys and the value as the value)
* @return string The response body
* @throws Exception Thrown if the request fails. The exception will either contain the cURL error code or the API error message.
*/
private function sendRequest($url, $reqType, $contentType, $payload = null, $curl_options = null)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $reqType,
CURLOPT_HTTPHEADER => [
"AccessKey: " . $this->apiAccessKey,
"Content-Type: " . $contentType
],
]);
if ($curl_options) {
foreach ($curl_options as $curl_option => $curl_value) {
curl_setopt($curl, $curl_option, $curl_value);
}
}
if ($payload) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
}
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
throw new Exception("cURL exception: " . $err);
} else {
switch ($responseCode) {
case 401:
throw new Exception("Unauthorized; check API key.");
case 200:
return $response;
break;
case 404:
throw new Exception("Not found.");
default:
throw new Exception("An unknown error occured. Status code: " . $responseCode);
}
}
}
/**
* Get video object from bunny
*
* @param string $videoId Video ID
* @return array Response
* @throws Exception Thrown if the video could not be retrieved. Contains the API error message.
*/
public function getVideo($videoId)
{
$url = $this->generateBaseUrl("/videos/" . $videoId);
try {
return json_decode($this->sendRequest($url, "GET", "application/json"), TRUE);
} catch (Exception $e) {
throw new Exception("Could not retrieve video. Error: " . $e->getMessage());
}
}
/**
* List videos from library
*
* @param int $page Page number
* @param int $perPage Number of videos per page
* @param string $sortBy Sort by field
* @param string $search Search by (optional)
* @param string $collection Collection (optional)
* @return array Response
* @throws Exception Thrown if the list of videos could not be retrieved. Contains the API error message.
*/
public function listVideos($page = 1, $perPage = 10, $sortBy = "date", $search = null, $collection = null)
{
$url = $this->generateBaseUrl("/videos");
$url .= "?" . http_build_query([
"page" => $page,
"per_page" => $perPage,
"sort_by" => $sortBy
]);
if ($search)
$url .= "&search=" . $search;
if ($collection)
$url .= "&collection=" . $collection;
try {
return json_decode($this->sendRequest($url, "GET", "application/json"), TRUE);
} catch (Exception $e) {
throw new Exception("Could not retrieve list of videos. Error: " . $e->getMessage());
}
}
/**
* Update existing video
*
* @param string $videoId Video ID
* @param string $title Title
* @param string $collectionId Collection ID
* @return array Response
* @throws Exception Thrown if the video could not be updated. Contains the API error message.
*/
public function updateVideo($videoId, $title, $collectionId)
{
$url = $this->generateBaseUrl("/videos/" . $videoId);
$payload = [
"title" => $title,
"collectionId" => $collectionId
];
try {
return json_decode($this->sendRequest($url, "POST", "application/json", json_encode($payload)), TRUE);
} catch (Exception $e) {
throw new Exception("Error updating video: " . $e->getMessage());
}
}
/**
* Delete existing video
*
* @param string $videoId Video ID
* @return array Response
* @throws Exception Thrown if the video could not be deleted. The exception will contain the API error message.
*/
public function deleteVideo($videoId)
{
$url = $this->generateBaseUrl("/videos/" . $videoId);
try {
return json_decode($this->sendRequest($url, "DELETE", "application/json"), TRUE);
} catch (Exception $e) {
throw new Exception("Could not delete video: " . $e->getMessage());
}
}
/**
* Create new video
*
* @param string $title Title
* @param string $collectionId Collection ID (optional)
* @return array Response
* @throws Exception Thrown if the video could not be created. The exception will contain the API error message.
*/
public function createVideo($title, $collectionId = null)
{
$url = $this->generateBaseUrl("/videos");
$payload = [
"title" => $title,
];
if ($collectionId) {
$payload["collectionId"] = $collectionId;
}
try {
return json_decode($this->sendRequest($url, "POST", "application/json", json_encode($payload)), TRUE);
} catch (Exception $e) {
throw new Exception("Could not create video. Error: " . $e->getMessage());
}
}
/**
* Upload video if the video ID is provided
*
* @param string $videoId Video ID
* @param string $filePath File path
* @return array Response
*/
public function uploadVideoWithVideoId($videoId, $filePath)
{
$url = $this->generateBaseUrl("/videos/" . $videoId);
if (!file_exists($filePath)) {
throw new Exception("File does not exist.");
}
try {
return json_decode($this->sendRequest($url, "PUT", "application/json", null, [
CURLOPT_PUT => 1,
CURLOPT_INFILE => fopen($filePath, "r"),
CURLOPT_INFILESIZE => filesize($filePath)
]), TRUE);
} catch (Exception $e) {
throw new Exception("Upload failed. Error: " . $e->getMessage());
}
}
/**
* Create video object and upload video
*
* @param string $title Title
* @param string $filePath File path
* @param string $collectionId Collection ID (optional)
* @return array Response
* @throws Exception Rethrows exceptions from createVideo/uploadVideoWithVideoId as required.
*/
public function uploadVideo($title, $filePath, $collectionId = null)
{
$videoObject = "";
try {
$videoObject = $this->createVideo($title, $collectionId);
return $this->uploadVideoWithVideoId($videoObject["guid"], $filePath);
} catch (Exception $e) {
throw $e;
}
}
/**
* Set video thumbnail
*
* @param string $videoId Video ID
* @param string $thumbnailUrl Thumbnail URL
* @return array Response
* @throws Exception Thrown if the thumbnail could not be set. The exception will contain the API error message.
*/
public function setVideoThumbnail($videoId, $thumbnailUrl)
{
$url = $this->generateBaseUrl("/videos/" . $videoId . "/thumbnail?thumbnailUrl=" . urlencode($thumbnailUrl));
$payload = [];
try {
return json_decode($this->sendRequest($url, "POST", "application/json", json_encode($payload)), TRUE);
} catch (Exception $e) {
throw new Exception("Could not set video thumbnail. Error: " . $e->getMessage());
}
}
/**
* Fetch video from external source
*
* @param string $videoId Video ID
* @param string $source Video URL
* @param array $headers HTTP headers to send while fetching video (optional)
* @return array Response
* @throws Exception Thrown if the video could not be fetched. The exception will contain the API error message.
*/
public function fetchVideo($videoId, $source, $headers = null)
{
$url = $this->generateBaseUrl("/videos/" . $videoId . "/fetch");
$payload = [
"url" => $source,
];
if ($headers) {
$payload["headers"] = $headers;
}
try {
return json_decode($this->sendRequest($url, "POST", "application/json", json_encode($payload)), TRUE);
} catch (Exception $e) {
throw new Exception("Could not fetch video. Error: " . $e->getMessage());
}
}
/**
* Add video captions
*
* @param string $videoId Video ID
* @param string $language Unique srclang shortcode
* @param string $captions Captions file
* @param string $label The text description label for the caption (optional)
* @return array Response
* @throws Exception Thrown if the captions could not be added. The exception will contain the API error message.
*/
public function addVideoCaptions($videoId, $language, $content, $label = null)
{
$url = $this->generateBaseUrl("/videos/" . $videoId . "/captions/" . $language);
$payload = [
"captionsFile" => base64_encode(file_get_contents($content)),
"srclang" => $language,
];
if ($label) {
$payload["label"] = $label;
}
try {
return json_decode($this->sendRequest($url, "POST", "application/json", json_encode($payload)), TRUE);
} catch (Exception $e) {
throw new Exception("Could not add captions. Error: " . $e->getMessage());
}
}
/**
* Delete caption
*
* @param string $videoId Video ID
* @param string $language Unique srclang shortcode
* @return array Response
* @throws Exception Thrown if the caption could not be deleted. The exception will contain the API error message.
*/
public function deleteVideoCaptions($videoId, $language)
{
$url = $this->generateBaseUrl("/videos/" . $videoId . "/captions/" . $language);
try {
return json_decode($this->sendRequest($url, "DELETE", "application/json"), TRUE);
} catch (Exception $e) {
throw new Exception("Could not delete captions. Error: " . $e->getMessage());
}
}
}
?>