Created
October 9, 2013 14:03
-
-
Save aymanfarhat/6901824 to your computer and use it in GitHub Desktop.
Youtube public API wrapper in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YoutubePublicAPI { | |
// Developers key to access the API | |
private $key; | |
private $base_url = "https://www.googleapis.com/youtube/v3/"; | |
public function __construct($params){ | |
$this->key = $params["key"]; | |
} | |
/** | |
* Gets the statistics data for a channel | |
* @param string $channel_id [description] | |
* @return associative array | |
*/ | |
public function getChannelStatistics($channel_id) | |
{ | |
$args["part"] = "statistics"; | |
$args["id"] = $channel_id; | |
$args["key"] = $this->key; | |
$result = $this->get("channels",$args); | |
$data = array(); | |
if($result) | |
{ | |
return get_object_vars($result->items[0]->statistics); | |
} | |
return $data; | |
} | |
/** | |
* Gets a list of playlists availabel for a channel | |
* @param [type] $channel_id [description] | |
* @return [type] [description] | |
*/ | |
public function getChannelPlaylists($channel_id) | |
{ | |
$data = array(); | |
$args["part"] = "snippet"; | |
$args["channelId"] = $channel_id; | |
$args["key"] = $this->key; | |
$result = $this->get("playlists",$args); | |
foreach($result->items as $channel_raw) | |
{ | |
$channel = array(); | |
$channel["channelId"] = $channel_id; | |
$channel["id"] = $channel_raw->id; | |
$channel["publishedAt"] = $channel_raw->snippet->publishedAt; | |
$channel["title"] = $channel_raw->snippet->title; | |
$channel["description"] = $channel_raw->snippet->description; | |
array_push($data, $channel); | |
} | |
return $data; | |
} | |
/** | |
* Gets a list of videos and their details beloning to a channel playlist | |
* @param string $channel_id [description] | |
* @return array of video ids | |
*/ | |
public function getPlaylistVideos($playlist_id) | |
{ | |
$data = array(); | |
$args["part"] = "snippet"; | |
$args["playlistId"] = $playlist_id; | |
$args["key"] = $this->key; | |
$result = $this->get("playlistItems",$args); | |
foreach($result->items as $videoObj) | |
{ | |
if($videoObj->snippet->resourceId->kind == "youtube#video") | |
{ | |
$vid = array(); | |
$vid["id"] = $videoObj->snippet->resourceId->videoId; | |
$vid["title"] = $videoObj->snippet->title; | |
array_push($data, $vid); | |
} | |
} | |
return $data; | |
} | |
/** | |
* Get the statistics for a youtube video | |
* @param [type] $video_id [description] | |
* @return [type] [description] | |
*/ | |
public function getVideoStatistics($video_id) | |
{ | |
$args["part"] = "statistics"; | |
$args["id"] = $video_id; | |
$args["key"] = $this->key; | |
$result = $this->get("videos",$args); | |
$data = array(); | |
if($result) | |
{ | |
return get_object_vars($result->items[0]->statistics); | |
} | |
} | |
/** | |
* Issues a get http request for fetching data based on the instance | |
* variables set in this class | |
* @param associative array $params holds the arguments of the get request | |
* @return object or collection of objects | |
*/ | |
private function get($target, $params) | |
{ | |
// Compose the request URL | |
$url = $this->base_url.$target."?"; | |
$last = end(array_keys($params)); | |
foreach($params as $key => $value) | |
{ | |
$amp = "&"; | |
if($last === $key) | |
$amp = ""; | |
$url .= "$key=$value".$amp; | |
} | |
// Issue the request | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment