PHP Youtube:缩略图机制上的查看计数/时间持续时间戳

时间:2012-03-28 21:47:28

标签: php xml youtube youtube-api

我在尝试使用DURATION时间显示缩略图时遇到困难。代码呈现缩略图但没有任何时间。此外,视图将以O视图的结果出现。其他所有东西都正在被正确获取。

    <?php
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads?max-results=2';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

      // get <yt:duration> node for video length
      $yt = $entry->children('http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads?max-results=2');
      $attrs = $yt->duration->attributes();


      // get <yt:stats> node for viewer statistics
        $yt = $entry->children('http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads?max-results=2');
        $attrs = $yt->statistics->attributes();
        $viewCount = $attrs['viewCount']; 
      ?>


      <div class="videoitem" style="height:">

        <div class="videotitle" style="float:right; width:220px; ">
            <h3 ><a href="<?php echo $watch; ?>" class="watchvideo"><?php echo $media->group->title; ?></a></h3>
            <p><?php 
      echo sprintf("%0.2f", $length/60) . " min. | 
        {$viewCount} views<br/>\n"; ?>
       </p>

        </div>
        <div class="videothumb" style="height:100px;"><a href="<?php echo $watch; ?>" class="watchvideo"><img src="<?php echo $thumbnail;?>" alt="<?php echo $media->group->title; ?>" width="150px" height="85px" /></a></div>

      </div>      
<?php $i++; if($i==3) { echo '<div class="clear small_v_margin"></div>'; $i=0; } } ?>

1 个答案:

答案 0 :(得分:3)

您将错误的URI传递给children方法。固定代码:

  // get <yt:duration> node for video length
  $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  $attrs = $yt->duration->attributes();
  $length = $attrs['seconds'];

  // get <yt:stats> node for viewer statistics
  $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
  $attrs = $yt->statistics->attributes();
  $viewCount = $attrs['viewCount'];