Skip to content

Instantly share code, notes, and snippets.

@hemache
Created November 9, 2012 21:20
Show Gist options
  • Save hemache/4048342 to your computer and use it in GitHub Desktop.
Save hemache/4048342 to your computer and use it in GitHub Desktop.
<?php
require_once('libs/curl/curl.php'); // https://github.com/shuber/curl
class Youtube extends Curl
{
public $params = array(
'key' => 'AIzaSyD2g6U5OzaLLvzEBBacZBhfaBIKeoZnKFM', // API Key
'maxResults' => 50, // Max items in result set
'part' => 'snippet' // https://developers.google.com/youtube/v3/getting-started#part
);
private $service_url;
public function __construct()
{
parent::__construct();
}
function get_playlist($playlist_id)
{
$this->service_url = 'https://www.googleapis.com/youtube/v3/playlistItems';
$this->params = array_merge($this->params, array(
'playlistId' => $playlist_id,
'part' => 'snippet,contentDetails'
));
$playlist = $this->get($this->service_url, $this->params)
->json();
$this->_fetch_items($playlist);
$this->_reset_params();
return $playlist;
}
function get_channel_playlists($channel_id)
{
$this->service_url = 'https://www.googleapis.com/youtube/v3/playlists';
$this->params = array_merge($this->params, array(
'channelId' => $channel_id
);
$playlists = $this->get($this->service_url, $this->params)
->json();
$this->_fetch_items($playlists);
$this->_reset_params();
return $playlists;
}
private function _fetch_items(&$object)
{
while($object['pageInfo']['totalResults'] > count($object['items']))
{
$this->params['pageToken'] = $object['nextPageToken'];
$object_part = $this->get($this->service_url, $this->params)
->json();
if(isset($object_part['nextPageToken']))
{
$object['nextPageToken'] = $object_part['nextPageToken'];
}
$object['items'] = array_merge($object['items'], $object_part['items']);
}
}
private function _reset_params()
{
$this->params = array(
'key' => 'AIzaSyD2g6U5OzaLLvzEBBacZBhfaBIKeoZnKFM', // API Key
'maxResults' => 50, // Max items in result set
'part' => 'snippet' // https://developers.google.com/youtube/v3/getting-started#part
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment