我可以使用 xml 从 youtube api 获取页面令牌吗?
Can I get page token from youtube api using xml?
我正在做 youtube api 并开发一个 youtube api 网站,我想获取视频列表的下一页标记,但我正在从 xml 提要中获取数据。
如何从 xml 供稿中获取下一页标记?
这是我的代码:
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
// get <gd:rating> node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
$my_format = 'mqdefault.jpg';
$my_url = 'img.ytapi.com';
?>
<div class="item">
<span class="title">
<a href="<?php echo $watch; ?>"><?php echo $media->group->title; ?></a>
</span>
<p>
<span class="thumbnail">
<a href="<?php echo $watch; ?>"><img src="<?php echo str_replace(array("i.ytimg.com","0.jpg"),array($my_url, $my_format), $thumbnail);?>" /></a>
<br/>click to view
</span>
<span class="attr">By:</span> <?php echo $entry->author->name; ?> <br/>
<span class="attr">Duration:</span> <?php printf('%0.2f', $length/60); ?>
min. <br/>
<span class="attr">Views:</span> <?php echo $viewCount; ?> <br/>
<span class="attr">Rating:</span> <?php echo $rating; ?>
</p>
</div>
<?php
}
?>
您可以使用 start-index
parameter. This parameter works in conjunction with max-results
参数请求其他页面。所以:
如果省略 max-results
参数,则请求这些 URL:
- http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed
结果 1-25
- http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?start-index=26
结果 26-50
- http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?start-index=51
结果 51-75
如果您设置 max-results=10
,则请求这些 URL:
我正在做 youtube api 并开发一个 youtube api 网站,我想获取视频列表的下一页标记,但我正在从 xml 提要中获取数据。
如何从 xml 供稿中获取下一页标记?
这是我的代码:
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
// get <gd:rating> node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
$my_format = 'mqdefault.jpg';
$my_url = 'img.ytapi.com';
?>
<div class="item">
<span class="title">
<a href="<?php echo $watch; ?>"><?php echo $media->group->title; ?></a>
</span>
<p>
<span class="thumbnail">
<a href="<?php echo $watch; ?>"><img src="<?php echo str_replace(array("i.ytimg.com","0.jpg"),array($my_url, $my_format), $thumbnail);?>" /></a>
<br/>click to view
</span>
<span class="attr">By:</span> <?php echo $entry->author->name; ?> <br/>
<span class="attr">Duration:</span> <?php printf('%0.2f', $length/60); ?>
min. <br/>
<span class="attr">Views:</span> <?php echo $viewCount; ?> <br/>
<span class="attr">Rating:</span> <?php echo $rating; ?>
</p>
</div>
<?php
}
?>
您可以使用 start-index
parameter. This parameter works in conjunction with max-results
参数请求其他页面。所以:
如果省略 max-results
参数,则请求这些 URL:
- http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed
结果 1-25 - http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?start-index=26
结果 26-50 - http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?start-index=51
结果 51-75
如果您设置 max-results=10
,则请求这些 URL: