我正在创建一个音乐网站,我想制作一个能够自动在我的文章旁边播放Youtube视频的功能。例如:如果文章标题是Pink Floyd而不是文章旁边显示来自Youtube搜索结果的第一个视频使用关键字“Pink Floyd”。我找到了这个https://developers.google.com/youtube/2.0/developers_guide_protocol_partial,但我不确定如何将它嵌入我的页面..任何人都可以帮助我
答案 0 :(得分:0)
这可能会对您有所帮助:
<?php
$q = "Pink Floyd";
$feedURL = "http://gdata.youtube.com/feeds/api/videos/?q={$q}&orderby=relevance&max-results=1";
$sxml = simplexml_load_file($feedURL);
// get summary counts from opensearch: namespace
$counts = $sxml-> children('http://a9.com/-/spec/opensearchrss/1.0/');
$total = $counts->totalResults;
$startOffset = $counts->startIndex;
$endOffset = ($startOffset-1) + $counts->itemsPerPage;
// iterate over entries in resultset
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$title=$media->group->title;
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
$linkarr=explode('watch?v=',$watch);
$linkarr1=explode('&',$linkarr[1]);
$link="https://www.youtube.com/embed/".$linkarr1[0];
// 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'];
if(($length - floor($length/60) * 60) <10){
$time= floor($length/60). ':0'.($length - floor($length/60) * 60);
}else{
$time= floor($length/60). ':'.($length -floor($length/60) * 60);
}
// 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;
}
?>
<div style="float:left; margin:0px 10px 5px 20px; width:492px; height:100px; ">
<div style="float:left; position:relative; width:100px; height:100px;">
<img src="<? echo($thumbnail)?>" width="100" height="100" style="cursor:pointer;" onclick='changefunc("<? echo($link);?>")'>
</div>
<div style="float:left; position:relative; width:380px; height:auto; font-size:15px; margin-left:5px; font-weight:bold; color:#000;">
<a href="javascript:void(null)" onclick='changefunc("<? echo($link);?>")' style="color:#000;"><? echo($title);?></a>
</div>
<div style="float:left;width:380px; height:auto; font-size:12px; margin:25px 0 0 5px; font-weight:bold; color:#000;">
Duration: <? echo($time);?>
</div>
</div>
<?
}?>