How to get latest videos from multiple Youtube channels in single API Call

送分小仙女□ 提交于 2020-01-15 12:26:06

问题


Am using the below PHP code to get the latest videos from a user channel,

$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER-ID/uploads?max-results=50'; $sxml = simplexml_load_file($feedURL);

Is there any way I can call the Youtube API and get the videos from different user channels in the single API call. And list the videos in desc order on a page.


回答1:


Sorry, you werent very clear on your answer.

First of get the total videos uploaded by calling the api with a json format:

   http://gdata.youtube.com/feeds/api/users/LinusTechTips/uploads?v=2&alt=jsonc

use:

   $json = json_decode($jsonString); 
   print($json->data->totalItems . "\n");

to get the viewcount, then you can calculate the rest!

   $totalItems = $json->data->totalItems;
   $maxResults = 50
   $xmlResults = array();
   for($i = $maxResults > $totalItems-50; $i+$maxResults) {
          $xmlResults[] = simplexml_load_file(
             'http://gdata.youtube.com/feeds/api/users/LinusTechTips/uploads?max-results=' . $i
          );
   }

To be clear; this is not tested. So im not sure it works; but this is the way to do it.

Note: XML is alot slower then json, I stronly advise you to use the json formatting!




回答2:


You could try to set up a array with usernames/ids and foreach them. For example:

$usernames = array(
   'xeonnnnn',
   'LinusTechTips'
);

$xmlResults = null;
foreach ($usernames as $user) {
   $xmlResults[] = simplexml_load_file(
     'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=50'
   );
}


来源:https://stackoverflow.com/questions/16789483/how-to-get-latest-videos-from-multiple-youtube-channels-in-single-api-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!