Jos*_*ody 2 php xml simplexml youtube-api
我正在使用simpleXML来解析这个xml文件.这是我用来访问YouTube API的Feed.我想将最新的视频嵌入到对象中并显示接下来的四个缩略图.
所以我正在使用simplexml_load_file它,使用foreach循环来访问每个feed中的值.
我可以访问这些值没有问题,但我遇到的问题是它将每个视频存储在一个单独的SimpleXMLElement Object.我无法控制我正在访问哪个对象,因为它们没有存储在数组中.所以我不能说,$thumb[4]或者说$entry[4]->thumb.
我尝试使用SimpleXMLIterator,但无论出于何种原因,具有相同开头的任何值都呈现为空白.例如,视频可能有以下十一种变体:
而这些将分别呈现为[1]="",[2]="",[3]="",等.
我很乐意为任何可以提供帮助的人提供更多信息!
编辑
这是我的结果的print_r.这是在一个变量上完成的,让您了解我面临的结构问题.整个print_r($ entry)将为XML文件中的每个节点提供变量.
SimpleXMLElement Object
(
[0] => 159
)
SimpleXMLElement Object
(
[0] => 44
)
Run Code Online (Sandbox Code Playgroud)
此外,print_r只是在PHP块内部进行测试.我实际上是想在HTML中的回声中访问变量.
您遇到了名称空间和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'>
Run Code Online (Sandbox Code Playgroud)
该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元素$feed->id,你需要的方法的SimpleXMLElement ::儿童() .它允许您指定要检索的子元素的命名空间.
例如,
$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;
Run Code Online (Sandbox Code Playgroud)
目前打印2010-02-06T05:23:33.858Z.
要获取可以使用的第一个条目元素的id echo $children->entry[0]->id;.
但是你会点击<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";
}
Run Code Online (Sandbox Code Playgroud)
(目前)打印
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
Run Code Online (Sandbox Code Playgroud)
编辑:我可能会在浏览器中使用更多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
);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:"当我点击缩略图时,我将使用jQuery在主空间加载视频.好像我需要精确访问节点[#],对吧?"
如果您已经在使用JavaScript/jQuery,那么PHP脚本(如果需要的话)可以简单地返回所有视频的所有数据的(JSON编码)数组,并且您的jQuery脚本可以找出如何处理数据.
| 归档时间: |
|
| 查看次数: |
6068 次 |
| 最近记录: |