How to get latest videos from multiple Youtube cha

2019-08-07 16:52发布

问题:

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'
   );
}