PHP:Youtube最新的视频源PHP代码机制

时间:2012-03-28 06:27:06

标签: php xml rss youtube

我目前正在使用我的网站使用的代码,但现在我想改变它的一些格式,但它比预期的任务更难。我的代码现在正在显示最新的视频。但我目前的目标是让代码显示视频*缩略图,*视频说明和*总观看次数。下面是我的代码,如果您认为有更好的方法来解决这个问题,那么我愿意接受建议:

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

&GT;

3 个答案:

答案 0 :(得分:3)

以Chris Willenbrock的工作为基础,为了简化代码并节省一些开销(额外的条目20- $计数在线上,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并尽可能地避免使用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>
  ";
}

答案 1 :(得分:1)

添加

var_dump($entry);exit;

在foreach代码内的第一行,然后查看输出并搜索缩略图。然后你必须按照路径($ entry-&gt; children(..)和$ media-&gt; path-&gt; to-&gt; thumbnail)来跟踪路径

答案 2 :(得分:0)

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++;
}