Search for videos in a YouTube channel using youtube API with php
Once you create a project in google developer console and grant nessary access to that project you can use the Clinet ID, email address (not your email address – this is a email address that get created associated with project) and .p12 file (you can download this file from google developer console and upload to a private location in your web server).
Download the Google PHP client library from GitHub. and include it in your script.
require_once 'Google/autoload.php'; require_once 'Google/Client.php'; require_once 'Google/Service/YouTube.php';
PHP code is like below, define the global variable
$app_name ='forsrilankans-LIVE'; $client_id = 'XXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com'; $Email_address = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@developer.gserviceaccount.com'; $key_file_location = dirname(dirname( dirname(__FILE__) )) .'/private/some-file-name.p12';
Accessing the google service.
$client = new Google_Client(); $client->setApplicationName($app_name); $key = file_get_contents($key_file_location); $scopes ="https://www.googleapis.com/auth/youtube.readonly"; $cred = new Google_Auth_AssertionCredentials($Email_address, array($scopes), $key); $client->setAssertionCredentials($cred); if($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $youtube = new Google_Service_YouTube($client); $videos = $youtube->search->listSearch('snippet', array( 'channelId' => 'UCFtEEv80fQVKkD4h1PF-Xqw', 'maxResults' => 50 , 'order' => 'date', 'q' => 'visual studio', 'type' => 'video', 'videoDuration' => 'any', ));