views:

322

answers:

1

I've got a nice little web app working that brings down playlists and videos from YouTube using the API. However, YouTube seems to fire back the playlists in the wrong order.

Any idea how I can do that?

Here is my very simple PHP code that does everything I need it to do (apart from date ordering)

    <?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/users/popcorncomedy/playlists?v=2';

// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>

<div class="container">
  <h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
  // get nodes in media: namespace for media information
  $media = $entry->children('http://search.yahoo.com/mrss/');

  // get <yt:duration> node for video length
  $yt = $entry->children('http://gdata.youtube.com/schemas/2007');

  ?>

  <a href="videos.php?playlist=<?php echo $yt->playlistId; ?>"><div class="item">
    <span class="title"><?php echo $entry->title; ?></span><br /><br />
    <span class="summary"><?php echo $entry->summary; ?></span>
  </div></a>
<?php
} // closes the loop
?>

The XML element that contains the date is called: published in the date format: 2010-03-03T17:37:34.000Z

Any thoughts on how to achieve this simply?

I can't find a function within the API itself that orders content for you.

+1  A: 

What you want is the orderby parameter http://code.google.com/apis/youtube/2.0/developers_guide_protocol_api_query_parameters.html#orderbysp however I haven't gotten it to work for playlists yet. As far as I can find its supports work with palylists but I never seen it work, hopefully you will have better luck!

Scott
Yeah i found that in the end. It works for ordering the videos themselves, but not actually the list of playlists. Weird huh!
Simon Hume
More like annoying but here's the note they put on it:Note: For a user's playlists or subscriptions feed, the default ordering is arbitrary.
Scott