patschi

Simple YouTube API v3 class

Apr 25th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2. //==> usage
  3. // youtube data retrieving
  4. $youtube = new YouTubeAPI();
  5. $youtube->setAPIKey($APIKey);
  6.  
  7. // all videos data
  8. $videos = $youtube->getLatestUploads("ChannelName");
  9. /*
  10.  Notice: sorted by date by default, latest on top
  11.          If you want to have the latest on the bottom,
  12.          just reverse the array using the command:
  13.            $videos = array_reverse($videos);
  14. */
  15.  
  16.  
  17. //==> class
  18. class YouTubeAPI {
  19.  
  20.     private $api;
  21.  
  22.     // API
  23.     public function setAPIKey($api)
  24.     {
  25.         $this->api = $api;
  26.     }
  27.  
  28.     public function getAPIKey()
  29.     {
  30.         return $this->api;
  31.     }
  32.  
  33.     // VIDEOS
  34.     public function getLatestUploads($channel)
  35.     {
  36.         $playlistId = $this->getPlaylistUploads($channel);
  37.         return $this->getPlaylistVideos($playlistId);
  38.     }
  39.  
  40.     public function getPlaylistUploads($username)
  41.     {
  42.         return $this->getRelatedPlaylists($username, "uploads");
  43.     }
  44.  
  45.     public function getPlaylistVideos($playlistId, $maxResults=30)
  46.     {
  47.         $url = $this->getUrl("playlistItems", array(
  48.             "part"       => "snippet",
  49.             "order"      => "date",
  50.             "maxResults" => $maxResults,
  51.             "playlistId" => $playlistId,
  52.         ));
  53.         $data = $this->getData($url);
  54.         return $data;
  55.     }
  56.  
  57.     public function getRelatedPlaylists($username=null, $playlist="uploads")
  58.     {
  59.         // possible values: likes, uploads
  60.         if($username === null) {
  61.             return null;
  62.         }
  63.  
  64.         $url = $this->getUrl("channels", array(
  65.             "part"        => "contentDetails",
  66.             "forUsername" => $username,
  67.         ));
  68.  
  69.         $data = $this->getData($url);
  70.  
  71.         $PID = @$data["items"][0]["contentDetails"]["relatedPlaylists"][$playlist];
  72.         if(empty($PID)) {
  73.             $PID = null;
  74.         }
  75.  
  76.         return $PID;
  77.     }
  78.  
  79.     // DATA
  80.     private function getUrl($service, $data)
  81.     {
  82.         if(empty($data["key"])) {
  83.             $data["key"] = $this->getAPIKey();
  84.         }
  85.         $query = http_build_query($data);
  86.         $url = sprintf("https://www.googleapis.com/youtube/v3/%s?%s", $service, $query);
  87.         return $url;
  88.     }
  89.  
  90.     private function getData($url)
  91.     {
  92.         $ch = curl_init();
  93.         curl_setopt_array($ch, array(
  94.             CURLOPT_URL => $url,
  95.             CURLOPT_PORT => 443,
  96.             CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
  97.             CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
  98.             CURLOPT_TIMEOUT => 10,
  99.             CURLOPT_CONNECTTIMEOUT => 5,
  100.             CURLOPT_AUTOREFERER => false,
  101.             CURLOPT_RETURNTRANSFER => true,
  102.             CURLOPT_USERAGENT => "pkern.at VideoFeedRetriever",
  103.         ));
  104.         $data = curl_exec($ch);
  105.         curl_close($ch);
  106.  
  107.         $json = json_decode($data, true);
  108.         if(is_array($json)) {
  109.             return $json;
  110.         }else{
  111.             return $data;
  112.         }
  113.     }
  114. }
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment