質問編集履歴

3

誤字

2021/06/07 06:09

投稿

mmm022
mmm022

スコア5

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,18 @@
36
36
 
37
37
  ### 補足情報(FW/ツールのバージョンなど)
38
38
 
39
+
40
+
41
+ php5.6
42
+
43
+ YouTubeAPI_v3
44
+
45
+ 利用ソース参考:https://www.codexworld.com/upload-video-to-youtube-using-php/
46
+
47
+
48
+
49
+ ```php
50
+
39
51
  ```upload.php
40
52
 
41
53
  <?php
@@ -468,10 +480,6 @@
468
480
 
469
481
  </html>```
470
482
 
471
-
472
-
473
- php5.6
483
+ コード
474
-
484
+
475
- YouTubeAPI_v3
485
+ ```
476
-
477
- 利用ソース参考:https://www.codexworld.com/upload-video-to-youtube-using-php/

2

ソース追加

2021/06/07 06:09

投稿

mmm022
mmm022

スコア5

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,438 @@
36
36
 
37
37
  ### 補足情報(FW/ツールのバージョンなど)
38
38
 
39
+ ```upload.php
40
+
41
+ <?php
42
+
43
+ // Include api config file
44
+
45
+ require_once 'config.php';
46
+
47
+
48
+
49
+ // Include database class
50
+
51
+ require_once 'DB.class.php';
52
+
53
+
54
+
55
+ // Create an object of database class
56
+
57
+ $db = new DB;
58
+
59
+
60
+
61
+ // If the form is submitted
62
+
63
+ if(isset($_POST['videoSubmit'])){
64
+
65
+ // Video info
66
+
67
+ $title = $_POST['title'];
68
+
69
+ $desc = $_POST['description'];
70
+
71
+ $tags = $_POST['tags'];
72
+
73
+ $privacy = !empty($_POST['privacy'])?$_POST['privacy']:'public';
74
+
75
+
76
+
77
+ // Check whether file field is not empty
78
+
79
+ if($_FILES["file"]["name"] != ''){
80
+
81
+ // File upload path
82
+
83
+ $fileName = str_shuffle('codexworld').'-'.basename($_FILES["file"]["name"]);
84
+
85
+ $filePath = "videos/".$fileName;
86
+
87
+
88
+
89
+ // Check the file type
90
+
91
+ $allowedTypeArr = array("video/mp4", "video/avi", "video/mpeg", "video/mpg", "video/mov", "video/wmv", "video/rm");
92
+
93
+ if(in_array($_FILES['file']['type'], $allowedTypeArr)){
94
+
95
+ // Upload file to local server
96
+
97
+ if(move_uploaded_file($_FILES['file']['tmp_name'], $filePath)){
98
+
99
+ // Insert video data in the database
100
+
101
+ $vdata = array(
102
+
103
+ 'title' => $title,
104
+
105
+ 'description' => $desc,
106
+
107
+ 'tags' => $tags,
108
+
109
+ 'privacy' => $privacy,
110
+
111
+ 'file_name' => $fileName
112
+
113
+ );
114
+
115
+ $insert = $db->insert($vdata);
116
+
117
+
118
+
119
+ // Store db row id in the session
120
+
121
+ $_SESSION['uploadedFileId'] = $insert;
122
+
123
+ }else{
124
+
125
+ header("Location:","https://powerb.co.jp/test/maika/fileup_v2/index.php?err=ue");
126
+
127
+ exit;
128
+
129
+ }
130
+
131
+ }else{
132
+
133
+ header("Location:","https://powerb.co.jp/test/maika/fileup_v2/index.php?err=fe");
134
+
135
+ exit;
136
+
137
+ }
138
+
139
+ }else{
140
+
141
+ header('Location:','https://powerb.co.jp/test/maika/fileup_v2/index.php?err=bf');
142
+
143
+ exit;
144
+
145
+ }
146
+
147
+ }
148
+
149
+
150
+
151
+ // Get uploaded video data from database
152
+
153
+ $videoData = $db->getRow($_SESSION['uploadedFileId']);
154
+
155
+
156
+
157
+ // Check if an auth token exists for the required scopes
158
+
159
+ $tokenSessionKey = 'token-' . $client->prepareScopes();
160
+
161
+ if (isset($_GET['code'])) {
162
+
163
+ if (strval($_SESSION['state']) !== strval($_GET['state'])) {
164
+
165
+ die('The session state did not match.');
166
+
167
+ }
168
+
169
+
170
+
171
+ $client->authenticate($_GET['code']);
172
+
173
+ $_SESSION[$tokenSessionKey] = $client->getAccessToken();
174
+
175
+ header('Location: ' . REDIRECT_URL);
176
+
177
+ }
178
+
179
+
180
+
181
+ if (isset($_SESSION[$tokenSessionKey])) {
182
+
183
+ $client->setAccessToken($_SESSION[$tokenSessionKey]);
184
+
185
+ }
186
+
187
+
188
+
189
+ // Check to ensure that the access token was successfully acquired.
190
+
191
+ if ($client->getAccessToken()) {
192
+
193
+ $htmlBody = '';
194
+
195
+ try{
196
+
197
+ // REPLACE this value with the path to the file you are uploading.
198
+
199
+ $videoPath = 'videos/'.$videoData['file_name'];
200
+
201
+
202
+
203
+ if(!empty($videoData['youtube_video_id'])){
204
+
205
+ // Uploaded video data
206
+
207
+ $videoTitle = $videoData['title'];
208
+
209
+ $videoDesc = $videoData['description'];
210
+
211
+ $videoTags = $videoData['tags'];
212
+
213
+ $videoId = $videoData['youtube_video_id'];
214
+
215
+ }else{
216
+
217
+ // Create a snippet with title, description, tags and category ID
218
+
219
+ // Create an asset resource and set its snippet metadata and type.
220
+
221
+ // This example sets the video's title, description, keyword tags, and
222
+
223
+ // video category.
224
+
225
+ $snippet = new Google_Service_YouTube_VideoSnippet();
226
+
227
+ $snippet->setTitle($videoData['title']);
228
+
229
+ $snippet->setDescription($videoData['description']);
230
+
231
+ $snippet->setTags(explode(",", $videoData['tags']));
232
+
233
+
234
+
235
+ // Numeric video category. See
236
+
237
+ // https://developers.google.com/youtube/v3/docs/videoCategories/list
238
+
239
+ $snippet->setCategoryId("22");
240
+
241
+
242
+
243
+ // Set the video's status to "public". Valid statuses are "public",
244
+
245
+ // "private" and "unlisted".
246
+
247
+ $status = new Google_Service_YouTube_VideoStatus();
248
+
249
+ $status->privacyStatus = $videoData['privacy'];
250
+
251
+
252
+
253
+ // Associate the snippet and status objects with a new video resource.
254
+
255
+ $video = new Google_Service_YouTube_Video();
256
+
257
+ $video->setSnippet($snippet);
258
+
259
+ $video->setStatus($status);
260
+
261
+
262
+
263
+ // Specify the size of each chunk of data, in bytes. Set a higher value for
264
+
265
+ // reliable connection as fewer chunks lead to faster uploads. Set a lower
266
+
267
+ // value for better recovery on less reliable connections.
268
+
269
+ $chunkSizeBytes = 1 * 1024 * 1024;
270
+
271
+
272
+
273
+ // Setting the defer flag to true tells the client to return a request which can be called
274
+
275
+ // with ->execute(); instead of making the API call immediately.
276
+
277
+ $client->setDefer(true);
278
+
279
+
280
+
281
+ // Create a request for the API's videos.insert method to create and upload the video.
282
+
283
+ $insertRequest = $youtube->videos->insert("status,snippet", $video);
284
+
285
+
286
+
287
+ // Create a MediaFileUpload object for resumable uploads.
288
+
289
+ $media = new Google_Http_MediaFileUpload(
290
+
291
+ $client,
292
+
293
+ $insertRequest,
294
+
295
+ 'video/*',
296
+
297
+ null,
298
+
299
+ true,
300
+
301
+ $chunkSizeBytes
302
+
303
+ );
304
+
305
+ $media->setFileSize(filesize($videoPath));
306
+
307
+
308
+
309
+
310
+
311
+ // Read the media file and upload it chunk by chunk.
312
+
313
+ $status = false;
314
+
315
+ $handle = fopen($videoPath, "rb");
316
+
317
+ while (!$status && !feof($handle)) {
318
+
319
+ $chunk = fread($handle, $chunkSizeBytes);
320
+
321
+ $status = $media->nextChunk($chunk);
322
+
323
+ }
324
+
325
+ fclose($handle);
326
+
327
+
328
+
329
+ // If you want to make other calls after the file upload, set setDefer back to false
330
+
331
+ $client->setDefer(false);
332
+
333
+
334
+
335
+ // Update youtube video id to database
336
+
337
+ $db->update($videoData['id'], $status['id']);
338
+
339
+
340
+
341
+ // Delete video file from local server
342
+
343
+ @unlink("videos/".$videoData['file_name']);
344
+
345
+
346
+
347
+ // uploaded video data
348
+
349
+ $videoTitle = $status['snippet']['title'];
350
+
351
+ $videoDesc = $status['snippet']['description'];
352
+
353
+ $videoTags = implode(",",$status['snippet']['tags']);
354
+
355
+ $videoId = $status['id'];
356
+
357
+ }
358
+
359
+
360
+
361
+ // uploaded video embed html
362
+
363
+ $youtubeURL = 'https://youtu.be/'.$videoId;
364
+
365
+ $htmlBody .= "<p class='succ-msg'>Video Uploaded to YouTube</p>";
366
+
367
+ $htmlBody .= '<embed width="400" height="315" src="https://www.youtube.com/embed/'.$videoId.'"></embed>';
368
+
369
+ $htmlBody .= '<ul><li><b>YouTube URL: </b><a href="'.$youtubeURL.'">'.$youtubeURL.'</a></li>';
370
+
371
+ $htmlBody .= '<li><b>Title: </b>'.$videoTitle.'</li>';
372
+
373
+ $htmlBody .= '<li><b>Description: </b>'.$videoDesc.'</li>';
374
+
375
+ $htmlBody .= '<li><b>Tags: </b>'.$videoTags.'</li></ul>';
376
+
377
+ $htmlBody .= '<a href="logout.php">Logout</a>';
378
+
379
+
380
+
381
+ } catch (Google_Service_Exception $e) {
382
+
383
+ $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
384
+
385
+ htmlspecialchars($e->getMessage()));
386
+
387
+ } catch (Google_Exception $e) {
388
+
389
+ $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
390
+
391
+ htmlspecialchars($e->getMessage()));
392
+
393
+ $htmlBody .= 'Please reset session <a href="logout.php">Logout</a>';
394
+
395
+ }
396
+
397
+
398
+
399
+ $_SESSION[$tokenSessionKey] = $client->getAccessToken();
400
+
401
+ } elseif (OAUTH_CLIENT_ID == '') {
402
+
403
+ $htmlBody = <<<END
404
+
405
+ <h3>Client Credentials Required</h3>
406
+
407
+ <p>
408
+
409
+ You need to set <code>$oauthClientID</code> and
410
+
411
+ <code>$oauthClientSecret</code> before proceeding.
412
+
413
+ <p>
414
+
415
+ END;
416
+
417
+ } else {
418
+
419
+ // If the user hasn't authorized the app, initiate the OAuth flow
420
+
421
+ $state = mt_rand();
422
+
423
+ $client->setState($state);
424
+
425
+ $_SESSION['state'] = $state;
426
+
427
+
428
+
429
+ $authUrl = $client->createAuthUrl();
430
+
431
+ $htmlBody = <<<END
432
+
433
+ <h3>Authorization Required</h3>
434
+
435
+ <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
436
+
437
+ END;
438
+
439
+ }
440
+
441
+ ?>
442
+
443
+
444
+
445
+ <!DOCTYPE html>
446
+
447
+ <html>
448
+
449
+ <head>
450
+
451
+ <title>Upload Video to YouTube using PHP by CodexWorld</title>
452
+
453
+ </head>
454
+
455
+ <body>
456
+
457
+ <div class="uplink"><a href="<?php echo BASE_URL; ?>">New Upload</a></div>
458
+
459
+ <div class="content">
460
+
461
+ <!-- Display uploaded video info -->
462
+
463
+ <?php echo $htmlBody; ?>
464
+
465
+ </div>
466
+
467
+ </body>
468
+
469
+ </html>```
470
+
39
471
 
40
472
 
41
473
  php5.6

1

誤字

2021/06/07 06:08

投稿

mmm022
mmm022

スコア5

test CHANGED
File without changes
test CHANGED
@@ -11,10 +11,6 @@
11
11
  エラー文の内容によると、おそらく割り当て上限に達したのだと思うのですが、一度きりのアップロードをテストで行っただけですので、本当に上限に達したとは考えづらいかと思います。。。
12
12
 
13
13
  何か他に考えられる要因がないかどうかお力添えいただきたく思います。
14
-
15
-
16
-
17
- ■■な機能を実装中に以下のエラーメッセージが発生しました。
18
14
 
19
15
 
20
16