PHP:Youtube最新视频提要PHP代码机制
我目前正在修改一个代码,以适应我的网站的使用,但现在我想改变它的一些格式,但这是一个比预期更困难的任务。我的代码现在正在显示最新的视频。但我目前的目标是让代码显示视频*缩略图图片,*视频描述和*总视图。下面是我的代码,如果您认为有更好的方法,那么我愿意接受建议:PHP:Youtube最新视频提要PHP代码机制,php,xml,rss,youtube,Php,Xml,Rss,Youtube,我目前正在修改一个代码,以适应我的网站的使用,但现在我想改变它的一些格式,但这是一个比预期更困难的任务。我的代码现在正在显示最新的视频。但我目前的目标是让代码显示视频*缩略图图片,*视频描述和*总视图。下面是我的代码,如果您认为有更好的方法,那么我愿意接受建议: <? error_reporting(E_ALL); $feedURL = 'http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?max-resul
<?
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;
}
添加
var_dump($entry);exit;
在foreach代码的第一行,查看输出并搜索缩略图。然后,您必须按照URL($entry->children(..)和$media->path->to->缩略图)的路径进行操作。添加
var_dump($entry);exit;
在foreach代码的第一行,查看输出并搜索缩略图。然后你必须按照URL的路径($entry->children(..)和$media->path->to->缩略图)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++;
}
//设置
$channel_name='mychannelname'//请务必将此更改为您的频道
$count=8;//#要显示的视频数量(最多20个)
$em_宽度=420//嵌入播放器的宽度
$em_高度=315//嵌入式播放器的高度
$wrap_class='video'//div包装器的类名
//输出。。。
错误报告(E_全部);
$feedURL='1http://gdata.youtube.com/feeds/api/users/'.$channel_name.'/uploads?最大结果=20';
$sxml=simplexml\u加载文件($feedURL);
$i=1;
foreach($sxml->entry as$entry){
$vidUrl=explode(“/”,$entry->id);
$vidKey=$vidUrl[6];
如果($iYoutube已经更新了他们的嵌入方法和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++;
}
//设置
$channel_name='mychannelname';//请确保将其更改为您的频道
$count=8;//#要显示的视频(最多20个)
$em_width=420;//嵌入播放器的宽度
$em_height=315;//嵌入式播放器的高度
$wrap_class='video';//div包装器的类名
//输出。。。
错误报告(E_全部);
$feedURL='1http://gdata.youtube.com/feeds/api/users/'.$channel_name.'/uploads?最大结果=20';
$sxml=simplexml\u加载文件($feedURL);
$i=1;
foreach($sxml->entry as$entry){
$vidUrl=explode(“/”,$entry->id);
$vidKey=$vidUrl[6];
如果($i以Chris Willenbrock的工作为基础,为了简化代码并节省一些开销(额外的条目20-$count,额外的分解20-$count条目,这些条目无论如何都不会显示):
//设置
$channel_name='mychannelname';//请确保将其更改为您的频道
$count=8;//#要显示的视频(最多20个)
$em_width=420;//嵌入式播放器的宽度
$em_height=315;//嵌入式播放器的高度
$wrap_class='video';//div包装器的类名
//输出。。。
$sxml=simplexml\u加载文件(“http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?最大结果=$count”);
foreach($sxml->entry as$entry){
$vidKey=substr(strrchr($entry->id,“/”),1);
回声“
";
}
我不喜欢使用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>
";
}
//输出。。。
$api_v2=”http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?最大结果=$count&v=2”;
foreach(json解码(文件获取内容($api\u v2&alt=json))->feed->entry as$entry){
//元信息
$title=$entry->title->{'$t'};
$description=$entry->{'media$group'}->{'media$description'}->{'t'};
$views=$entry->{'yt$statistics'}->viewCount;
$thumbnails=$entry->{'media$group'}->{'media$thumbnails'};
//这里有几种不同的缩略图选择:
//0=>默认图像,低分辨率-“默认”
//1=>默认图像,中等分辨率-“mqdefault”
//2=>默认图像,更高分辨率-“hqdefault”
//3=>视频的第一帧,低分辨率-“开始”
//4=>中间帧,低分辨率-“中间”
//5=>最后一帧,低分辨率-“结束”
$thumb\u img=$thumbnails[1];//我将使用默认值,中等分辨率
回声“
url'style='float:left;margin-right:10px;'width='$thumb\u img->width'height='$thumb\u img->height'alt='{$thumb\u img->{'yt$name'}>
标题:$Title
Description:$Description
视图:$Views
";
}
以Chris Willenbrock的工作为基础,为了简化代码并节省一些开销(额外的条目20-$count,额外的分解20-$count条目,这些条目无论如何都不会显示):
//设置
$channel_name='mychannelname';//请确保将其更改为您的频道
$count=8;//#要显示的视频(最多20个)
$em_width=420;//嵌入式播放器的宽度
$em_height=315;//嵌入式播放器的高度
$wrap_class='video';//div包装器的类名
//输出。。。
$sxml=simplexml\u加载文件(“http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?最大结果=$count”);
foreach($sxml->entry as$entry){
$vidKey=substr(strrchr($entry->id,“/”),1);
回声“
";
}
我不喜欢使用XML,尽量避免使用它,所以这里有另一个使用JSON的选项