YouTube API Get Logged In User Channel Information In PHP
After a long research to get logged in user information of Youtube channel, Only this solution is working for me. So i am sharing here it will help someone. You can use your google account to login with authentication to get Youtube channel information. Let's see step by step.
Step1: Go to google developer page and create new project to get CLIENT_ID and CLIENT_SECRET
Step2: Please ensure that you have enabled the YouTube Data API for your project.
Step3: Create index.php and add following code.
Demo & Download
Using this Function you can get Youtube channel information using access token
Step1: Go to google developer page and create new project to get CLIENT_ID and CLIENT_SECRET
Step2: Please ensure that you have enabled the YouTube Data API for your project.
Step3: Create index.php and add following code.
Demo & Download
require_once 'src/Google_Client.php'; require_once 'src/contrib/Google_YoutubeService.php'; session_start(); $OAUTH2_CLIENT_ID = 'XXXXXXXXXXXXXXXX'; $OAUTH2_CLIENT_SECRET = 'XXXXXXXXXXXXXXXX'; $client = new Google_Client(); $client->setClientId($OAUTH2_CLIENT_ID); $client->setClientSecret($OAUTH2_CLIENT_SECRET); $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL); $client->setRedirectUri($redirect); $youtube = new Google_YoutubeService($client); if (isset($_GET['code'])) { if (strval($_SESSION['state']) !== strval($_GET['state'])) { die('The session state did not match.'); } $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); header('Location: ' . $redirect); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } $json_output = json_decode($client->getAccessToken()); $token = $json_output->access_token; if ($client->getAccessToken()) { $userid = getYoutubeData($token); $htmlBody = "SubscriberCount: ".youtubefollowers($userid); $_SESSION['token'] = $client->getAccessToken(); } else { $state = mt_rand(); $client->setState($state); $_SESSION['state'] = $state; $authUrl = $client->createAuthUrl(); $htmlBody = <<<END <h3>Connect to youtube</h3> <p>You need to <a href="$authUrl">Connect</a> before proceeding.<p> END; }
function getYoutubeData($token) { $json_url ='https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&access_token='.$token; $json = file_get_contents($json_url); $json_output = json_decode($json); if($json_output->items[0]->id){ $id = $json_output->items[0]->id; }else{ $id = ""; } return $id; }This Function will return Youtube channel subscriber using channel ID
function youtubefollowers($channelID) { $json_url ='https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.$channelID.'&key=AIzaSyAlgOkFu2KlYJAQwt5j3jO1rUARpPzAIww'; $json = file_get_contents($json_url); $json_output = json_decode($json); if($json_output->items[0]->statistics->subscriberCount){ $subscriberCount = $json_output->items[0]->statistics->subscriberCount; }else{ $subscriberCount = 0; } return $subscriberCount; }Now you can display output as following
<!doctype html> <html> <head> <title>YouTube</title> </head> <body> <?=$htmlBody?> </body> </html>
Thanks for visiting my blog. Feel free to share your feedback.
Please support us, Like us on Facebook.
Subscribe to:
Post Comments (Atom)
Hi,
ReplyDeleteyour code give me error:
Notice: Trying to get property of non-object in D:\xampp-portable\htdocs\xampp\ernik\live\HK_Youtube\index.php on line 44
Hi,
ReplyDeleteIs there any way to get current user information like email firstname, lastname? I want to use login option using Youtube in my website.
thanks
ReplyDeletethanks
ReplyDelete