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


PHP Youtube: View Count/Time Duration stamp on the thumbnail mechanism

我在尝试显示时间为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; } } ?>

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

  // 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'];