我目前正在调整一个代码以适应我的网站使用,但现在我想更改它的一些格式,但这是一项比预期更艰巨的任务。我的代码现在正在显示最新的视频。但我目前的目标是让代码显示视频*缩略图、*视频描述和*总浏览量。下面是我的代码,如果你认为有更好的方法来解决这个问题,那么我愿意接受建议:
<?
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?max-results=20';
$sxml = simplexml_load_file($feedURL);
$i = 0;
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>';
break;
}
?>
为了在Chris Willenbrock的工作基础上进行构建,为了稍微简化代码并节省一些开销(额外的条目20-$count跨线,对无论如何都不会显示的20-$count条目进行额外的分解):
//SETTINGS
$channel_name = 'mychannelname';//Be sure to change this to your channel
$count = 8;//# of videos you want to show (MAX = 20)
$em_width = 420;//width of embedded player
$em_height = 315;//height of embedded player
$wrap_class = 'video';//class name for the div wrapper
//The output...
$sxml = simplexml_load_file("http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?max-results=$count");
foreach ($sxml->entry as $entry) {
$vidKey = substr(strrchr($entry->id,'/'),1);
echo "
<div class='"$wrap_class'">
<iframe width='"$em_width'" height='"$em_height'" src='"http://www.youtube.com/embed/$vidKey'" frameborder='"0'" allowfullscreen></iframe>
</div>
";
}
我不喜欢使用XML,尽可能避免使用它,所以这里有另一个使用JSON的选项。此外,请注意,通过在这里切换到API的v2,我们可以更干净地访问视频密钥,以及原始海报所要求的其他元数据:
//The output...
$api_v2 = "http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?max-results=$count&v=2";
foreach (json_decode(file_get_contents("$api_v2&alt=json"))->feed->entry as $entry) {
// meta information
$title = $entry->title->{'$t'};
$description = $entry->{'media$group'}->{'media$description'}->{'$t'};
$views = $entry->{'yt$statistics'}->viewCount;
$thumbnails = $entry->{'media$group'}->{'media$thumbnail'};
// few different thumbnail image choice here:
// 0 => default image, low res - "default"
// 1 => default image, medium res - "mqdefault"
// 2 => default image, higher res - "hqdefault"
// 3 => first frame of vid, low res - "start"
// 4 => middle frame, low res - "middle"
// 5 => last frame, low res - "end"
$thumb_img = $thumbnails[1]; // I'll go with default, medium res
echo "
<!-- meta information output - format to taste -->
<div>
<img src='$thumb_img->url' style='float: left; margin-right: 10px;' width='$thumb_img->width' height='$thumb_img->height' alt='{$thumb_img->{'yt$name'}}'>
<b>Title:</b> $title<br><br>
<b>Description:</b> $description<br><br>
<b>Views:</b> $views
<br style='clear: left;'>
</div>
<div class='"$wrap_class'">
<iframe width='"$em_width'" height='"$em_height'" src='"{$entry->content->src}'" frameborder='"0'" allowfullscreen></iframe>
</div>
";
}
添加
var_dump($entry);exit;
在foreach代码的第一行,查看输出并搜索缩略图。然后,您必须按照URL的路径($entry->children(..)和$media->path->to->thumbnail)
Youtube已经更新了他们的嵌入方法和api输出,所以我抓住机会更新了脚本。你可能可以把所有的设置都移到剧本的核心部分,但我想我会把它拉出来,让它更容易理解。希望这有帮助:
//SETTINGS
$channel_name = 'mychannelname';//Be sure to change this to your channel
$count = 8;//# of videos you want to show (MAX = 20)
$em_width = 420;//width of embeded player
$em_height = 315;//height of embeded player
$wrap_class = 'video';//class name for the div wrapper
//The output...
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/'.$channel_name.'/uploads?max-results=20';
$sxml = simplexml_load_file($feedURL);
$i = 1;
foreach ($sxml->entry as $entry) {
$vidUrl = explode("/", $entry->id);
$vidKey = $vidUrl[6];
if ($i <= $count ) :
echo '
<div class="'.$wrap_class.'">
<iframe width="'.$em_width.'" height="'.$em_height.'" src="http://www.youtube.com/embed/'.$vidKey.'" frameborder="0" allowfullscreen></iframe>
</div>
';
endif;
$i++;
}