YouTube API 3从特定用户获取最近的视频



我需要帮助从新的API版本的特定用户检索YouTube视频。

我已经在console.developers.google.com上创建了YouTube Data API

OAuth对于浏览器的公共API访问

在我使用这个代码检索最新的6个视频之前:

$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;
}

如何用API 3更新此代码?

我一直在。net中做类似的事情,它不像以前那么简单了。

现在需要几个额外的步骤:

步骤1:您需要通过:

获取用户的channelId

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={0}&key={1} -其中{0}是用户名,密钥是您的API密钥

第二步:你可以通过:

得到一个视频列表

https://www.googleapis.com/youtube/v3/search?order=date&部分= snippet& channelId ={0},关键= {1}

步骤3:

https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,player&id={0}&key={1} -其中id为步骤2返回的videoId。

希望对你有帮助。

更新答案- 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&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>

这实际上是一个两步的过程。

https://developers.google.com/youtube/v3/guides/implementation/videos videos-retrieve-uploads

步骤1:检索频道上传视频的播放列表ID

步骤2:检索上传的视频列表

您可以链接这些请求。所以第二个请求可以是1。

最新更新