问题
I need help to retrieve YouTube videos from specific user with the new API version.
I've create YouTube Data API on console.developers.google.com
Both OAuth and Public API access for Browser.
Before I was use this code to retrieve the latest 6 video:
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?max-results=6';
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$watch = (string)$media->group->player->attributes()->url;
}
How can I update this code with API 3?
回答1:
I've been working on a similar thing in .net and its not quite as simple as it used to be.
It now requires a couple of extra steps:
Step 1: you need to get the channelId for the user via:
https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={0}&key={1} - where {0} is the USERNAME and key is you API key
Step 2: from that you can get a list of videos via:
https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={0}&key={1}
Step 3:
https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,player&id={0}&key={1} - where id is the videoId returned from step 2.
Hope that helps.
回答2:
Updated answer - 2018
<?php
/**
* Gets the latest YouTube channel video.
*/
class Youtube
{
/**
* Channel ID or Channel User.
*
* @access private
* @var string
*/
private $channel = "";
/**
* Class constructor.
*
* @param string $channel Channel ID or Channel User.
* @access public
*/
public function __construct($channel)
{
$this->channel = $channel;
}
/**
* Gets the video.
*
* @access public
* @return array
*/
public function video() : array
{
return array( 'title' => $this->getVideo('title'), 'id' => $this->getVideo('id') );
}
/**
* Gets the last video.
*
* @param string $property 'title' or 'id'
* @access private
* @return string
*/
private function getVideo($property) : string
{
$xml = null;
if ( @fopen('https://www.youtube.com/feeds/videos.xml?user=' . $this->channel, 'r') !== false ) {
$xml = simplexml_load_file('https://www.youtube.com/feeds/videos.xml?user=' . $this->channel); // Channel User
} elseif ( @fopen('https://www.youtube.com/feeds/videos.xml?channel_id=' . $this->channel, 'r') !== false ) {
$xml = simplexml_load_file('https://www.youtube.com/feeds/videos.xml?channel_id=' . $this->channel); // Channel ID
}
if ($xml !== null) {
$namespaces = $xml->getNamespaces(true);
$video = $xml->entry[0]->children($namespaces['yt']);
if ($property === 'title') {
return $xml->entry[0]->title;
} elseif ($property === 'id') {
return $video->videoId;
}
} else {
return "";
}
}
}
$channel = 'UChsXToK8E4U8lqXaabPwlBw'; // Channel ID or Channel User.
$Youtube = new Youtube($channel);
$video = $Youtube->video();
?>
<h1><?php echo $video['title']; ?></h1>
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $video['id']; ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
回答3:
It's actually a 2 step process.
https://developers.google.com/youtube/v3/guides/implementation/videos#videos-retrieve-uploads
Step 1: Retrieve the playlist ID for the channel's uploaded videos
Step 2: Retrieve the list of uploaded videos
You can chain these requests. So 2nd request could be 1.requests's callback function.
来源:https://stackoverflow.com/questions/30393289/youtube-api-3-get-last-videos-from-specific-user