Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit f37fb9f

Browse files
Add file for coming-soon PHP quickstart guide.
1 parent 48b4230 commit f37fb9f

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

php/quickstart.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
// Sample PHP code for user authorization
4+
5+
// Call set_include_path() as needed to point to your client library.
6+
require_once 'Google/autoload.php';
7+
require_once 'Google/Client.php';
8+
require_once 'Google/Service/YouTube.php';
9+
session_start();
10+
11+
/*
12+
* This variable specifies the location of a file where the access and
13+
* refresh tokens will be written after successful authorization.
14+
* Please ensure that you have enabled the YouTube Data API for your project.
15+
*/
16+
define('CREDENTIALS_PATH', '~/php-yt-oauth2.json');
17+
18+
function getClient() {
19+
$client = new Google_Client();
20+
// Set to name/location of your client_secrets.json file.
21+
$client->setAuthConfigFile('client_secrets.json');
22+
// Set to valid redirect URI for your project.
23+
$client->setRedirectUri('http://localhost');
24+
25+
$client->addScope(GOOGLE_SERVICE_YOUTUBE::YOUTUBE_READONLY);
26+
$client->setAccessType('offline');
27+
28+
// Load previously authorized credentials from a file.
29+
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
30+
if (file_exists($credentialsPath)) {
31+
$accessToken = file_get_contents($credentialsPath);
32+
} else {
33+
// Request authorization from the user.
34+
$authUrl = $client->createAuthUrl();
35+
printf("Open the following link in your browser:\n%s\n", $authUrl);
36+
print 'Enter verification code: ';
37+
$authCode = trim(fgets(STDIN));
38+
39+
// Exchange authorization code for an access token.
40+
$accessToken = $client->authenticate($authCode);
41+
42+
// Store the credentials to disk.
43+
if(!file_exists(dirname($credentialsPath))) {
44+
mkdir(dirname($credentialsPath), 0700, true);
45+
}
46+
file_put_contents($credentialsPath, $accessToken);
47+
printf("Credentials saved to %s\n", $credentialsPath);
48+
}
49+
$client->setAccessToken($accessToken);
50+
51+
// Refresh the token if it's expired.
52+
if ($client->isAccessTokenExpired()) {
53+
$client->refreshToken($client->getRefreshToken());
54+
file_put_contents($credentialsPath, $client->getAccessToken());
55+
}
56+
return $client;
57+
}
58+
59+
/**
60+
* Expands the home directory alias '~' to the full path.
61+
* @param string $path the path to expand.
62+
* @return string the expanded path.
63+
*/
64+
function expandHomeDirectory($path) {
65+
$homeDirectory = getenv('HOME');
66+
if (empty($homeDirectory)) {
67+
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
68+
}
69+
return str_replace('~', realpath($homeDirectory), $path);
70+
}
71+
72+
// Define an object that will be used to make all API requests.
73+
$client = getClient();
74+
$service = new Google_Service_YouTube($client);
75+
76+
if (isset($_GET['code'])) {
77+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
78+
die('The session state did not match.');
79+
}
80+
81+
$client->authenticate($_GET['code']);
82+
$_SESSION['token'] = $client->getAccessToken();
83+
header('Location: ' . $redirect);
84+
}
85+
86+
if (isset($_SESSION['token'])) {
87+
$client->setAccessToken($_SESSION['token']);
88+
}
89+
90+
if (!$client->getAccessToken()) {
91+
print("no access token, whaawhaaa");
92+
exit;
93+
}
94+
95+
// Call channels.list to retrieve information
96+
97+
function channelsListByUsername($service, $part, $params) {
98+
$params = array_filter($params);
99+
$response = $service->channels->listChannels(
100+
$part,
101+
$params
102+
);
103+
104+
$description = sprintf(
105+
'This channel\'s ID is %s. Its title is %s, and it has %s views.',
106+
$response['items'][0]['id'],
107+
$response['items'][0]['snippet']['title'],
108+
$response['items'][0]['statistics']['viewCount']);
109+
print $description . "\n";
110+
}
111+
112+
channelsListByUsername($service, 'snippet,contentDetails,statistics', array('forUsername' => 'GoogleDevelopers'));
113+
?>

0 commit comments

Comments
 (0)