user1261800
user1261800

Reputation:

PHP: Youtube latest video feed php code mechanism

I am currently adapted a code to my website's use but now I would like to change some of its format but it has been a harder task than expected. My code right now is displaying the latest video. But my goal at the moment is to have the code display the videos *thumbnail pic, *video description and *total views. Below is my code, If you think there is a better way to approach this then I am open for suggestions:

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

?>

Upvotes: 0

Views: 5185

Answers (3)

To build on Chris Willenbrock's work, in order to simplify the code a little and save yourself some overhead (an extra entries 20-$count across the wire, extra explode on 20-$count entries that won't be displayed anyway):

//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>
  ";
}

I don't enjoy working with XML and avoid it where I can, so here is another option that uses JSON. Also, note that by switching to v2 of the API here we get much cleaner access to the video key, as well as the other meta-data that the original poster was asking for:

//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>
  ";
}

Upvotes: 3

Chris Willenbrock
Chris Willenbrock

Reputation: 1

Youtube has updated their embed methods and api output, so I took the opportunity to update the script. You can probably just move all of the settings into the core part of the script, but I figured I'd pull it out to make it easier to follow. Hope this helps:

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

Upvotes: 0

peipst9lker
peipst9lker

Reputation: 605

Add

var_dump($entry);exit;

at the first line inside the foreach code, then take a look at the output and search for your thumbnail images. Then you have to follow the path like it was done with the URL ($entry->children(..) and $media->path->to->thumbnail)

Upvotes: 0

Related Questions