如何更新YouTube视频? | PHP

时间:2018-04-29 21:15:14

标签: php youtube youtube-api youtube-data-api

我尝试更新YouTube视频。我在网站https://developers.google.com/youtube/v3/code_samples/php#update_a_video使用该功能,我尝试在我的代码中复制它,但如果我调用listVideos(),我会收到Google_Service_YouTube_VideoListResponse,但我需要一个Google_Service_YouTube_Video。我怎么能得到它?

我的PHP代码:

$youtube = new Google_Service_YouTube($client);
$videoid = "******Video ID********";
$new = "******* TEXT *********";
$videoinfo = $youtube->videos->listVideos('snippet', array('id' => $videoid));
$videoSnippet = $videoinfo[0]['snippet'];
$description = $videoSnippet['description'] . $new;
$videoSnippet['description'] = $description;
$youtube->videos->update("snippet", $videoSnippet);

错误:

: Uncaught TypeError: Argument 2 passed to 
Google_Service_YouTube_Resource_Videos::update() must be an instance of 
Google_Service_YouTube_Video, instance of 
Google_Service_YouTube_VideoSnippet given, called in 
P:\Apache24\htdocs\*********** on line 232 and defined in 
P:\Apache24\htdocs\*******\Google Api System\vendor\google\apiclient- 
services\src\Google\Service\YouTube\Resource\Videos.php:309 
Stack trace:
0 P:\Apache24\htdocs\*********: Google_Service_YouTube_Resource_Videos- 
>update('snippet', Object(Google_Service_YouTube_VideoSnippet ))
1 P:\Apache24\htdocs\********\*********\index.php(438): 
require('P:\\Apache24\\htd...')

1 个答案:

答案 0 :(得分:0)

你必须使用

  $video = $listResponse[0];
  $updateResponse = $youtube->videos->update("snippet", $video);

因为documentation

// Since the request specified a video ID, the response only
      // contains one video resource.
      $video = $listResponse[0];
      $videoSnippet = $video['snippet'];
      $tags = $videoSnippet['tags'];

      // Preserve any tags already associated with the video. If the video does
      // not have any tags, create a new list. Replace the values "tag1" and
      // "tag2" with the new tags you want to associate with the video.
      if (is_null($tags)) {
        $tags = array("tag1", "tag2");
      } else {
        array_push($tags, "tag1", "tag2");
      }

      // Set the tags array for the video snippet
      $videoSnippet['tags'] = $tags;

      // Update the video resource by calling the videos.update() method.
      $updateResponse = $youtube->videos->update("snippet", $video);