YouTube API - 使用 PHP 和 xml 用户供稿检索上次上传的视频
YouTube API - using PHP and xml user feed to retrieve last video uploads
所以我试图通过 Feed 获取最近 5 个 YouTube 用户上传的内容,最后我在网上找到了这段代码:
function yt_last_5() {
for($i = 0; $i < 5; ){
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . yt_user_id(). '/uploads?max-results=5';
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$url = (string)$media->group->player->attributes()->url;
$index = strrpos($url, "&");
$url = substr($url, 0, $index);
$index = strrpos($url, "watch");
$url = substr($url, 0, $index) . "v/" . substr($url, $index + 8, strlen($url) - ($index + 8));
echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="' . $url . '" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="400" height="250" src="' . $url . '" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />';
break;
}
$i++;
}
}
问题是它显示了最后上传的视频 5 次,实际上我希望它检索最后 5 个视频而不是重复一个。
最后一句话:非常感谢!
你有两个循环。
一个从 0 数到 5:
for($i = 0; $i < 5; )
// some code
$i++; // this could just be in the for(), by the way
在那里面,你有一些代码每次都做同样的事情,忽略计数器。其中包含一个依次查看每个视频的循环:
foreach ($sxml->entry as $entry) {
但在它有机会查看除第一个条目以外的任何内容之前,您跳出了内部循环:
break;
您只需要一个循环。
使用计数器方法,您可以使用 $i
来引用 XML 中的特定条目:
$sxml = simplexml_load_file($feedURL);
for($i = 0; $i < 5; $i++) {
$entry = $sxml->entry[$i];
// display a video
}
请注意,如果条目少于 5 个,这将失败;你可以通过测试 isset($sxml->entry[$i])
.
来解决这个问题
使用 foreach
循环,您可以计算回显了多少个视频,break
当您到达第 5 个时:
$sxml = simplexml_load_file($feedURL);
$i = 0;
foreach ($sxml->entry as $entry) {
$i++;
// display a video
if ( $i == 5 ) {
break;
}
}
所以我试图通过 Feed 获取最近 5 个 YouTube 用户上传的内容,最后我在网上找到了这段代码:
function yt_last_5() {
for($i = 0; $i < 5; ){
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . yt_user_id(). '/uploads?max-results=5';
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$url = (string)$media->group->player->attributes()->url;
$index = strrpos($url, "&");
$url = substr($url, 0, $index);
$index = strrpos($url, "watch");
$url = substr($url, 0, $index) . "v/" . substr($url, $index + 8, strlen($url) - ($index + 8));
echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="' . $url . '" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="400" height="250" src="' . $url . '" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />';
break;
}
$i++;
}
}
问题是它显示了最后上传的视频 5 次,实际上我希望它检索最后 5 个视频而不是重复一个。
最后一句话:非常感谢!
你有两个循环。
一个从 0 数到 5:
for($i = 0; $i < 5; )
// some code
$i++; // this could just be in the for(), by the way
在那里面,你有一些代码每次都做同样的事情,忽略计数器。其中包含一个依次查看每个视频的循环:
foreach ($sxml->entry as $entry) {
但在它有机会查看除第一个条目以外的任何内容之前,您跳出了内部循环:
break;
您只需要一个循环。
使用计数器方法,您可以使用 $i
来引用 XML 中的特定条目:
$sxml = simplexml_load_file($feedURL);
for($i = 0; $i < 5; $i++) {
$entry = $sxml->entry[$i];
// display a video
}
请注意,如果条目少于 5 个,这将失败;你可以通过测试 isset($sxml->entry[$i])
.
使用 foreach
循环,您可以计算回显了多少个视频,break
当您到达第 5 个时:
$sxml = simplexml_load_file($feedURL);
$i = 0;
foreach ($sxml->entry as $entry) {
$i++;
// display a video
if ( $i == 5 ) {
break;
}
}