我正在尝试开发包含一些YouTube视频的网站。从API中检索XML文件后,我有以下内容。
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<id>http://gdata.youtube.com/feeds/api/videos/4ZsiqqOyWx8</id>
<published>2007-08-03T05:48:51.000Z</published>
[...]
<author>
<name>ak326</name>
<uri>http://gdata.youtube.com/feeds/api/users/ak326</uri>
</author>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/4ZsiqqOyWx8/comments' countHint='0'/>
</gd:comments>
<media:group>
[...]
<yt:duration seconds='222'/>
</media:group>
<gd:rating average='5.0' max='5' min='1' numRaters='4' rel='http://schemas.google.com/g/2005#overall'/>
<yt:statistics favoriteCount='8' viewCount='2674'/>
</entry>
我正在尝试使用PHP检索此视频的长度,但使用
echo $xml->media->yt
但它不起作用。我认为它与媒体上的psuedo类有关,但我不知道如何选择它们。
答案 0 :(得分:1)
这些XML元素是命名空间。您需要get the namespace information。
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
答案 1 :(得分:1)
尝试DOMXPath
$xml = new DOMDocument();
$xml->load(path/to/file);
$xpath = new DOMXPath($xml);
$xpath->registerNamespace("atom", "http://www.w3.org/2005/Atom");
$xpath->registerNamespace("media", "http://search.yahoo.com/mrss/");
$xpath->registerNamespace("yt", "http://gdata.youtube.com/schemas/2007");
print $xpath->query("/atom:entry/media:group/yt:duration/@seconds")->item(0)->value;
答案 2 :(得分:0)
我假设你在这里使用SimpleXML
$nsMedia = $xml->children('http://search.yahoo.com/mrss/');
$group = $nsMedia->group;
$nsYt = $group->children('http://gdata.youtube.com/schemas/2007');
$duration = $nsYt->duration;
echo $duration['seconds'];