user1261800
user1261800

Reputation:

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

I am having difficulties trying displaying a thumbnail with time DURATION. The code renders a thumbnail but without any times. Also, the views are coming out with a result of O views. Everything else is being fetch properly.

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

Upvotes: 0

Views: 1852

Answers (1)

galymzhan
galymzhan

Reputation: 5523

You're passing wrong URI to the children method. Fixed code:

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

Upvotes: 2

Related Questions