Skip to content
Jubair Saidi edited this page Jun 24, 2016 · 3 revisions

(under construction)

Query the Data API

// Return a std PHP object 
$video = $youtube->getVideoInfo('rie-hPVJ7Sw');

// Return a std PHP object
$channel = $youtube->getChannelByName('xdadevelopers');

// Return a std PHP object
$channel = $youtube->getChannelById('UCk1SpWNzOs4MYmr0uICEntg');

// Return a std PHP object
$playlist = $youtube->getPlaylistById('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

// Return an array of PHP objects
$playlists = $youtube->getPlaylistsByChannelId('UCk1SpWNzOs4MYmr0uICEntg');

// Return an array of PHP objects
$playlistItems = $youtube->getPlaylistItemsByPlaylistId('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

// Return an array of PHP objects
$activities = $youtube->getActivitiesByChannelId('UCk1SpWNzOs4MYmr0uICEntg');

// Parse Youtube URL into videoId
$videoId = $youtube->parseVIdFromURL('https://www.youtube.com/watch?v=moSFlvxnbgk');
// result: moSFlvxnbgk

Search

// Search playlists, channels and videos, Return an array of PHP objects
$results = $youtube->search('Android');

// Search only Videos, Return an array of PHP objects
$videoList = $youtube->searchVideos('Android');

// Search only Videos in a given channel, Return an array of PHP objects
$videoList = $youtube->searchChannelVideos('keyword', 'UCk1SpWNzOs4MYmr0uICEntg', 50);

$results = $youtube->searchAdvanced(array( /* params */ ));

Basic Search Pagination

use Madcoda\Youtube;

$youtube = new Youtube(array('key' => '/* Your API key here */'));

// Set Default Parameters
$params = array(
    'q'             => 'Android',
    'type'          => 'video',
    'part'          => 'id, snippet',
    'maxResults'    => 50
);

// Make Intial Call. With second argument to reveal page info such as page tokens.
$search = $youtube->searchAdvanced($params, true);

// check if we have a pageToken
if (isset($search['info']['nextPageToken'])) {
    $params['pageToken'] = $search['info']['nextPageToken'];
}

// Make Another Call and Repeat
$search = $youtube->searchAdvanced($params, true);          

// add results key with info parameter set
print_r($search['results']); 

/* Alternative approach with new built in paginateResults function */
 
// Same Params as before
$params = array(
    'q'             => 'Android',
    'type'          => 'video',
    'part'          => 'id, snippet',
    'maxResults'    => 50
);

// an array to store page tokens so we can go back and forth
$pageTokens   = array();

// make inital search
$search       = $youtube->paginateResults($params, null);

// store token
$pageTokens[] = $search['info']['nextPageToken'];

// go to next page in result
$search       = $youtube->paginateResults($params, $pageTokens[0]);

// store token
$pageTokens[] = $search['info']['nextPageToken'];

// go to next page in result
$search       = $youtube->paginateResults($params, $pageTokens[1]);

// store token
$pageTokens[] = $search['info']['nextPageToken'];

// go back a page
$search       = $youtube->paginateResults($params, $pageTokens[0]);

// add results key with info parameter set
print_r($search['results']);

The pagination above is quite basic. Depending on what you are trying to achieve; you may want to create a recursive function that traverses the results.

Clone this wiki locally