开发者

Ordering content from the YouTube API with PHP & XML

开发者 https://www.devze.com 2022-12-24 01:53 出处:网络
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.

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 med开发者_开发知识库ia 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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消