通过 API 获取 YouTube 用户的完整播放列表列表
所以,这是我获取 YouTube 用户的公共播放列表的代码:
function getyoutubeplaylists($userName) {
$yt = connectyoutube();
$yt->setMajorProtocolVersion(2);
$playlistListFeed = $yt->getPlaylistListFeed($userName);
foreach ($playlistListFeed as $playlistListEntry) {
$playlist['title'] = $playlistListEntry->title->text;
$playlist['id'] = $playlistListEntry->getPlaylistID();
$playlists[] = $playlist;
$playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
foreach ($playlistVideoFeed as $videoEntry) {
$playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11);
$playlist_assignment['id'] = $playlist['id'];
$playlist_assignments[] = $playlist_assignment;
}
}
$everything['playlists'] = $playlists;
$everything['playlist_assignments'] = $playlist_assignments;
return $everything;
}
问题是,这只获取第一页或结果。关于如何使用 Zend Gdata 检索下一页结果有什么想法吗?
原始 gdata XML 显示所需的 URL:
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&max-results=25"/>
但是,getPlaylistListFeed 似乎没有任何参数来指定“start-index”或“max-results”。
So, here's my code for getting a youtube user's public playlists:
function getyoutubeplaylists($userName) {
$yt = connectyoutube();
$yt->setMajorProtocolVersion(2);
$playlistListFeed = $yt->getPlaylistListFeed($userName);
foreach ($playlistListFeed as $playlistListEntry) {
$playlist['title'] = $playlistListEntry->title->text;
$playlist['id'] = $playlistListEntry->getPlaylistID();
$playlists[] = $playlist;
$playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
foreach ($playlistVideoFeed as $videoEntry) {
$playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11);
$playlist_assignment['id'] = $playlist['id'];
$playlist_assignments[] = $playlist_assignment;
}
}
$everything['playlists'] = $playlists;
$everything['playlist_assignments'] = $playlist_assignments;
return $everything;
}
Problem is, this only gets the first page or results. Any ideas on how to use the Zend Gdata to retrieve the next page of results?
The raw gdata XML shows the URLs needed:
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&max-results=25"/>
However, getPlaylistListFeed doesn't seem to have any parameters to specify "start-index" or "max-results".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于我浪费了一整天“只是想在我的网站上显示我的视频列表”,我想我应该粘贴一些示例代码,这些代码教会了我很多东西。正如预期的那样,
Zend_Gdata_YouTube::
已经在 Magento 安装中。下面的代码将呈现一个统一的“视频”列表,其中播放列表在列表中与其他视频并排显示相同。 更新:我根据我的答案制作了一个 Magento 扩展。只是一个通用的 php,我放入 var/export 中。在顶部,您必须包含
app/Mage.php
。在这里,您在
$playlistListFeed
中有一个 Zend 对象数组。现在,在播放列表中,我查看该播放列表的视频并收集每个视频的统计数据。我将他们的时间相加以获得总播放时间,然后捕获播放列表中视频的最高观看次数和评分。
我最终使用 XML 来获取缩略图数据。
播放列表已处理完毕,现在让我们获取不在播放列表中的剩余视频:
这里我们有一个 YouTube 视频数组,顶部是按最旧的顺序排列的播放列表,后面是未出现在相同播放列表中的用户视频最旧的第一订单。所以我找到了这个简单的排序代码来改变顺序。这是一篇关于排序的好文章,如果您在这里尝试对多维数组进行排序,那么值得一读。
下面是帮助我开始使用这些愚蠢的 GData YouTube Zend 对象的代码:
下面是两个用于显示结构的数组条目。请注意,播放列表有一个
videos
键,其中包含视频数组。测试videos
键可以告诉您这是一个播放列表。url
是您的用户可以点击打开 YouTube 网站上的视频或播放列表的地址。最后,你可以从
show_methods()
中得到这样的东西:Since I wasted all day "just trying to get a list of my videos to show on my website", I thought I'd paste up some sample code that just taught me a lot. And as expected,
Zend_Gdata_YouTube::
is already in Magento installations. The code below will present a unified list of "videos", where playlists appear the same in the list as the other videos alongside each other. Update: I made a Magento extension out of my answer.This is just a generic php I dropped into var/export. At the top, you have to include
app/Mage.php
.Here, you've got an array of Zend objects in
$playlistListFeed
.Now, within the playlist, I look at the videos of this playlist and gather stats on each one. I sum their times to get a total playtime and then capture the highest view count and rating from the videos in the playlist.
I ended up using the XML to get the thumbnail data.
The playlists are handled, now let's get the remaining videos that aren't in playlists:
Here we have an array of our youtube videos, at the top are the playlsits ordered by oldest first, followed by the user's videos not appearing in playlists in same oldest first order. So I found this simple sort code to change the order. Great article there about sorting and worth a read if you're here trying to sort multidimensional arrays.
Here is the code that helped me get started working with these goofy GData YouTube Zend objects:
Here are two of the array entries to show the structure. Note that playlists have a
videos
key with an array of the videos inside it. Testing for thevideos
key can tell you it's a playlist. Theurl
is what your user can click on to open the video or playlist on the youtube site.And finally, the kind of stuff you get out of
show_methods()
:这是一个有用的 Zend 函数,不仅可以检索下一页,还可以检索提要的所有可用条目。
This is a useful Zend function for retrieving not only the next page but all available entries for a feed.
听起来您想要遵循有关分页的开发指南示例 。
It sounds like you want to follow the Dev Guide example on pagination.