You ran into the problem of namespaces and SimpleXML. The feed starts with
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
The xmlns='http://www.w3.org/2005/Atom'
sets the default namespace to http://www.w3.org/2005/Atom
. I.e. its child elements are not really id
, updated
, category
but http://www.w3.org/2005/Atom:id
, http://www.w3.org/2005/Atom:updated
and so on...
So you can't access the the id element via $feed->id, you need the method SimpleXMLELement::children(). It let's you specify the namespace of the child elements you want to retrieve.
E.g.
$feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
$children = $feed->children('http://www.w3.org/2005/Atom');
echo $children->updated;
(currently) prints 2010-02-06T05:23:33.858Z
To get the id of the first entry element you can use echo $children->entry[0]->id;
.
But then you'll hit the <media:group>
element and its children <media:category
, <media:player>
and so on... which are in the xmlns:media='http://search.yahoo.com/mrss/'
namespace.
$feed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?author=ofcoursegolf&max-results=5&prettyprint=true');
$group = $feed->children('http://www.w3.org/2005/Atom')
->entry[0]
->children('http://search.yahoo.com/mrss/')
->group
->children('http://search.yahoo.com/mrss/')
;
echo $group->player->attributes()->url, "\n";
foreach( $group->thumbnail as $thumb) {
echo 'thumb: ', $thumb->attributes()->url, "\n";
}
(currently) prints
http://www.youtube.com/watch?v=ikACkCpJ-js&feature=youtube_gdata
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/2.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/1.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/3.jpg
thumb: http://i.ytimg.com/vi/ikACkCpJ-js/0.jpg
edit: I'd probably do that within the browser with a lot more javascript but here's a (simple, ugly) example app
<?php
//define('TESTENV' , true);
function getFeed($author) {
// <-- add caching here if needed -->
if ( !defined('TESTENV') ) {
$url = sprintf('http://gdata.youtube.com/feeds/api/videos?author=%s&max-results=5',
urlencode($author)
);
}
else {
$url = 'feed.xml';
}
return simplexml_load_file($url);
}
$feed = getFeed('jonlajoie');
if ( !isset($_GET['id']) ) {
$selected = '';
}
else {
$selected = $_GET['id'];
printf ('
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/%s"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always">
</param>
<embed src="http://www.youtube.com/v/%s" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
</object>',
htmlspcialchars($selected), htmlspcialchars($selected)
);
}
$item = 0;
foreach( $feed->children('http://www.w3.org/2005/Atom')->entry as $entry ) {
$entryElements = $entry->children('http://www.w3.org/2005/Atom');
$groupElements = $entry
->children('http://search.yahoo.com/mrss/')
->group
->children('http://search.yahoo.com/mrss/')
;
if ( !preg_match('!^http://gdata.youtube.com/feeds/api/videos/([^/]+)!', $entryElements->id, $m) ) {
// google can choose whatever id they want. But this is only a simple example....
die('unexpected id: '.htmlspecialchars($entryElements->id));
}
$id = $m[1];
if ( $selected!==$id ) {
printf('<a href="?id=%s"><img src="%s" /></a>',
urlencode($id),
$groupElements->thumbnail[0]->attributes()->url
);
}
}
edit2: "And when I click the thumbnails, I'll use jQuery to load the video in the main space. Seems like I'll need precise access to node[#], right?"
If you're already using javascript/jquery your php script (if needed at all) could simply return a (json encoded) array of all the data for all the video and your jquery script could figure out what to do with the data.