名前空間と SimpleXMLの問題に遭遇しました。フィードは次で始まります
<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'>
はxmlns='http://www.w3.org/2005/Atom'
、デフォルトの名前空間を に設定しますhttp://www.w3.org/2005/Atom
。つまり、その子要素は実際id
には ,updated
ではcategory
なくhttp://www.w3.org/2005/Atom:id
,http://www.w3.org/2005/Atom:updated
などです...
したがって、 を介して id 要素にアクセスすることはできません。メソッドSimpleXMLELement::children()$feed->id
が必要です。取得する子要素の名前空間を指定できます。
例えば、
$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;
現在印刷され2010-02-06T05:23:33.858Z
ます。
最初のエントリ要素の ID を取得するには、 を使用できますecho $children->entry[0]->id;
。しかし、その後、名前空間にあるelement とその children などにヒット
します。<media:group>
<media:category
<media:player>
xmlns:media='http://search.yahoo.com/mrss/'
$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";
}
(現在) 版画
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
編集: 私はおそらく、より多くの JavaScript を使用してブラウザー内でそれを行うでしょうが、ここに (単純で醜い) サンプルアプリを示します。
<?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
);
}
}
編集 2: 「サムネイルをクリックすると、jQuery を使用してビデオをメイン スペースにロードします。ノード [#] への正確なアクセスが必要になるようですよね?」
すでに JavaScript/jQuery を使用している場合、PHP スクリプト (必要な場合) は、すべてのビデオのすべてのデータの (JSON エンコードされた) 配列を返すだけで、jQuery スクリプトはデータの処理方法を理解できます。