需要一些帮助,使用 PHP、YouTube API v3 和 google-api-php-client-1.0.0-Alpha 在 YouTube 上编辑我的视频。
# Set variables.
$category_id=27;
$description='';
$title='Test Title 1';
$availability=1;
# Instantiate a new instance of the Video class.
$video_obj=new Video();
# Get the YouTube instance. Starts the YouTubeService if it's not already started.
$yt=$video_obj->getYouTubeObject();
# Get Google Client
$client=$video_obj->getGoogleClient();
# Create a video list request.
$listResponse=$yt->listVideos('snippet,status', array('id' => $youtube_video_id));
$videoList=$listResponse['items'];
# Since a unique video id is given, it will only return 1 video.
$video=$videoList[0];
$videoSnippet=$video['snippet'];
$videoStatus=$video['status'];
# Create a snippet with title, description, tags and category ID
# Create an asset resource and set its snippet metadata and type.
# This example sets the video's title, description, keyword tags, and
# video category.
$updateSnippet=new Google_Service_YouTube_VideoSnippet($videoSnippet);
# Numeric video category. See
# https://developers.google.com/youtube/v3/docs/videoCategories/list
$updateSnippet->setCategoryId($category_id);
$updateSnippet->setDescription($description);
$updateSnippet->setTitle($title);
# Create a video status with privacy status. Valid statuses are "public", "private" and "unlisted".
$updateStatus=new Google_Service_YouTube_VideoStatus($videoStatus);
# Set video privacy. If Availability is 1 then it's a public video. Else it's private.
$updateStatus->privacyStatus=(($availability==1) ? "public" : "private");
# Associate the snippet and status objects with a new video resource.
$updateVideo=new Google_Service_YouTube_Video($video);
$updateVideo->setSnippet($updateSnippet);
$updateVideo->setStatus($updateStatus);
# Setting the defer flag to true tells the client to return a request which can be called
# with ->execute(); instead of making the API call immediately.
$client->setDefer(TRUE);
# Create a video update request.
# $yt->updateVideo is a wrapper method for $youtube->videos->update
$update_response=$yt->updateVideo('snippet,status', $updateVideo);
# If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(FALSE);
print_r($update_response);
exit;
响应:
Google_Http_Request Object
(
[batchHeaders:Google_Http_Request:private] => Array
(
[Content-Type] => application/http
[Content-Transfer-Encoding] => binary
[MIME-Version] => 1.0
)
[queryParams:protected] => Array
(
[part] => snippet,status
[key] => MY_KEY
)
[requestMethod:protected] => PUT
[requestHeaders:protected] => Array
(
[content-type] => application/json; charset=UTF-8
[authorization] => Bearer AUTH_RANDOMNESS
)
[baseComponent:protected] => https://www.googleapis.com
[path:protected] => /youtube/v3/videos
[postBody:protected] => {"snippet":{"categoryId":"27","title":"Test Title 1"},"status":{"privacyStatus":"public"}}
[userAgent:protected] =>
[responseHttpCode:protected] =>
[responseHeaders:protected] =>
[responseBody:protected] =>
[expectedClass:protected] => Google_Service_YouTube_Video
[accessKey] =>
)
下面是YouTube的github示例的示例。 https://github.com/youtube/api-samples/blob/master/php/update_video.php