【问题标题】:Automatically shows a youtube video自动显示 youtube 视频
【发布时间】:2014-01-19 00:24:35
【问题描述】:

我正在制作一个音乐网站,我想制作一个功能,在我的文章旁边自动投放 Youtube 视频。例如:如果文章标题是 Pink Floyd,则文章旁边会使用关键字“Pink Floyd”显示来自 Youtube 搜索结果的第一个视频。我找到了这个https://developers.google.com/youtube/2.0/developers_guide_protocol_partial,但我不知道如何将它嵌入到我的页面上。谁能帮助我

【问题讨论】:

    标签: video youtube


    【解决方案1】:

    这可能会对您有所帮助:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2015-10-01
      • 2012-05-26
      • 2020-01-11
      相关资源
      最近更新 更多