Skip to content

Commit a1691bb

Browse files
"Added sample: php/create_broadcast.php"
1 parent 8213bb5 commit a1691bb

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

php/create_broadcast.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
// Call set_include_path() as needed to point to your client library.
4+
require_once 'Google/Client.php';
5+
require_once 'Google/Service/YouTube.php';
6+
session_start();
7+
8+
/*
9+
* You can acquire an OAuth 2.0 client ID and client secret from the
10+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
11+
* For more information about using OAuth 2.0 to access Google APIs, please see:
12+
* <https://developers.google.com/youtube/v3/guides/authentication>
13+
* Please ensure that you have enabled the YouTube Data API for your project.
14+
*/
15+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
16+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
17+
18+
$client = new Google_Client();
19+
$client->setClientId($OAUTH2_CLIENT_ID);
20+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
21+
$client->setScopes('https://www.googleapis.com/auth/youtube');
22+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
23+
FILTER_SANITIZE_URL);
24+
$client->setRedirectUri($redirect);
25+
26+
// Define an object that will be used to make all API requests.
27+
$youtube = new Google_Service_YouTube($client);
28+
29+
if (isset($_GET['code'])) {
30+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
31+
die('The session state did not match.');
32+
}
33+
34+
$client->authenticate($_GET['code']);
35+
$_SESSION['token'] = $client->getAccessToken();
36+
header('Location: ' . $redirect);
37+
}
38+
39+
if (isset($_SESSION['token'])) {
40+
$client->setAccessToken($_SESSION['token']);
41+
}
42+
43+
// Check to ensure that the access token was successfully acquired.
44+
if ($client->getAccessToken()) {
45+
try {
46+
// Create an object for the liveBroadcast resource's snippet. Specify values
47+
// for the snippet's title, scheduled start time, and scheduled end time.
48+
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
49+
$broadcastSnippet->setTitle('New Broadcast');
50+
$broadcastSnippet->setScheduledStartTime('2034-01-30T00:00:00.000Z');
51+
$broadcastSnippet->setScheduledEndTime('2034-01-31T00:00:00.000Z');
52+
53+
// Create an object for the liveBroadcast resource's status, and set the
54+
// broadcast's status to "private".
55+
$status = new Google_Service_YouTube_LiveBroadcastStatus();
56+
$status->setPrivacyStatus('private');
57+
58+
// Create the API request that inserts the liveBroadcast resource.
59+
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
60+
$broadcastInsert->setSnippet($broadcastSnippet);
61+
$broadcastInsert->setStatus($status);
62+
$broadcastInsert->setKind('youtube#liveBroadcast');
63+
64+
// Execute the request and return an object that contains information
65+
// about the new broadcast.
66+
$broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
67+
$broadcastInsert, array());
68+
69+
// Create an object for the liveStream resource's snippet. Specify a value
70+
// for the snippet's title.
71+
$streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
72+
$streamSnippet->setTitle('New Stream');
73+
74+
// Create an object for content distribution network details for the live
75+
// stream and specify the stream's format and ingestion type.
76+
$cdn = new Google_Service_YouTube_CdnSettings();
77+
$cdn->setFormat("1080p");
78+
$cdn->setIngestionType('rtmp');
79+
80+
// Create the API request that inserts the liveStream resource.
81+
$streamInsert = new Google_Service_YouTube_LiveStream();
82+
$streamInsert->setSnippet($streamSnippet);
83+
$streamInsert->setCdn($cdn);
84+
$streamInsert->setKind('youtube#liveStream');
85+
86+
// Execute the request and return an object that contains information
87+
// about the new stream.
88+
$streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
89+
$streamInsert, array());
90+
91+
// Bind the broadcast to the live stream.
92+
$bindBroadcastResponse = $youtube->liveBroadcasts->bind(
93+
$broadcastsResponse['id'],'id,contentDetails',
94+
array(
95+
'streamId' => $streamsResponse['id'],
96+
));
97+
98+
$htmlBody .= "<h3>Added Broadcast</h3><ul>";
99+
$htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
100+
$broadcastsResponse['snippet']['title'],
101+
$broadcastsResponse['snippet']['publishedAt'],
102+
$broadcastsResponse['id']);
103+
$htmlBody .= '</ul>';
104+
105+
$htmlBody .= "<h3>Added Stream</h3><ul>";
106+
$htmlBody .= sprintf('<li>%s (%s)</li>',
107+
$streamsResponse['snippet']['title'],
108+
$streamsResponse['id']);
109+
$htmlBody .= '</ul>';
110+
111+
$htmlBody .= "<h3>Bound Broadcast</h3><ul>";
112+
$htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
113+
$bindBroadcastResponse['id'],
114+
$bindBroadcastResponse['contentDetails']['boundStreamId']);
115+
$htmlBody .= '</ul>';
116+
117+
} catch (Google_ServiceException $e) {
118+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
119+
htmlspecialchars($e->getMessage()));
120+
} catch (Google_Exception $e) {
121+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
122+
htmlspecialchars($e->getMessage()));
123+
}
124+
125+
$_SESSION['token'] = $client->getAccessToken();
126+
} else {
127+
// If the user hasn't authorized the app, initiate the OAuth flow
128+
$state = mt_rand();
129+
$client->setState($state);
130+
$_SESSION['state'] = $state;
131+
132+
$authUrl = $client->createAuthUrl();
133+
$htmlBody = <<<END
134+
<h3>Authorization Required</h3>
135+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
136+
END;
137+
}
138+
?>
139+
140+
<!doctype html>
141+
<html>
142+
<head>
143+
<title>Bound Live Broadcast</title>
144+
</head>
145+
<body>
146+
<?=$htmlBody?>
147+
</body>
148+
</html>

0 commit comments

Comments
 (0)