Skip to content

Commit 2d4d357

Browse files
"Added sample: php/update_video.php"
1 parent 76daecb commit 2d4d357

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

php/update_video.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/**
4+
* This sample adds new tags to a YouTube video by:
5+
*
6+
* 1. Retrieving the video resource by calling the "youtube.videos.list" method
7+
* and setting the "id" parameter
8+
* 2. Appending new tags to the video resource's snippet.tags[] list
9+
* 3. Updating the video resource by calling the youtube.videos.update method.
10+
*
11+
* @author Ibrahim Ulukaya
12+
*/
13+
14+
// Call set_include_path() as needed to point to your client library.
15+
require_once 'Google_Client.php';
16+
require_once 'contrib/Google_YouTubeService.php';
17+
session_start();
18+
19+
/*
20+
* You can acquire an OAuth 2.0 client ID and client secret from the
21+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
22+
* For more information about using OAuth 2.0 to access Google APIs, please see:
23+
* <https://developers.google.com/youtube/v3/guides/authentication>
24+
* Please ensure that you have enabled the YouTube Data API for your project.
25+
*/
26+
$OAUTH2_CLIENT_ID = 'REPLACE ME';
27+
$OAUTH2_CLIENT_SECRET = 'REPLACE ME';
28+
29+
$client = new Google_Client();
30+
$client->setClientId($OAUTH2_CLIENT_ID);
31+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
32+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
33+
FILTER_SANITIZE_URL);
34+
$client->setRedirectUri($redirect);
35+
36+
// Define an object that will be used to make all API requests.
37+
$youtube = new Google_YoutubeService($client);
38+
39+
if (isset($_GET['code'])) {
40+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
41+
die('The session state did not match.');
42+
}
43+
44+
$client->authenticate();
45+
$_SESSION['token'] = $client->getAccessToken();
46+
header('Location: ' . $redirect);
47+
}
48+
49+
if (isset($_SESSION['token'])) {
50+
$client->setAccessToken($_SESSION['token']);
51+
}
52+
53+
// Check to ensure that the access token was successfully acquired.
54+
if ($client->getAccessToken()) {
55+
try{
56+
57+
// REPLACE this value with the video ID of the video being updated.
58+
$videoId = "VIDEO_ID";
59+
60+
// Call the API's videos.list method to retrieve the video resource.
61+
$listResponse = $youtube->videos->listVideos("snippet",
62+
array('id' => $videoId));
63+
64+
$videoList = $listResponse['items'];
65+
66+
// If the videoList variable is empty, the specified video was not found.
67+
if (empty($videoList)) {
68+
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
69+
} else {
70+
// Since the request specified a video ID, the response only
71+
// contains one video resource.
72+
$video = $videoList[0];
73+
$videoSnippet = $video['snippet'];
74+
75+
$tags = $videoSnippet['tags'];
76+
77+
// Preserve any tags already associated with the video. If the video does
78+
// not have any tags, create a new list. Replace the values "tag1" and
79+
// "tag2" with the new tags you want to associate with the video.
80+
if (is_null($tags)) {
81+
$tags = array("tag1", "tag2");
82+
} else {
83+
array_push($tags, "tag1", "tag2");
84+
}
85+
86+
// Construct the video resource, using the updated tags, to send in the
87+
// videos.update API request.
88+
$updateVideo = new Google_Video($video);
89+
$updateSnippet = new Google_VideoSnippet($videoSnippet);
90+
$updateSnippet->setTags($tags);
91+
$updateVideo -> setSnippet($updateSnippet);
92+
93+
// Update the video resource by calling the videos.update() method.
94+
$updateResponse = $youtube->videos->update("snippet", $updateVideo);
95+
96+
$responseTags = $updateResponse['snippet']['tags'];
97+
98+
99+
$htmlBody .= "<h3>Video Updated</h3><ul>";
100+
$htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>',
101+
array_pop($responseTags), array_pop($responseTags),
102+
$videoId, $updateResponse['snippet']['title']);
103+
104+
$htmlBody .= '</ul>';
105+
}
106+
} catch (Google_ServiceException $e) {
107+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
108+
htmlspecialchars($e->getMessage()));
109+
} catch (Google_Exception $e) {
110+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
111+
htmlspecialchars($e->getMessage()));
112+
}
113+
114+
$_SESSION['token'] = $client->getAccessToken();
115+
} else {
116+
// If the user hasn't authorized the app, initiate the OAuth flow
117+
$state = mt_rand();
118+
$client->setState($state);
119+
$_SESSION['state'] = $state;
120+
121+
$authUrl = $client->createAuthUrl();
122+
$htmlBody = <<<END
123+
<h3>Authorization Required</h3>
124+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
125+
END;
126+
}
127+
?>
128+
129+
<!doctype html>
130+
<html>
131+
<head>
132+
<title>Video Updated</title>
133+
</head>
134+
<body>
135+
<?=$htmlBody?>
136+
</body>
137+
</html>

0 commit comments

Comments
 (0)