2014-01-19 58 views

回答

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> 
<? 
}?>