在 PHP 中检索 XML 伪类值?

发布于 2024-11-15 08:37:29 字数 1227 浏览 0 评论 0原文

我正在尝试开发一个包含一些 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

但它不起作用。我认为这与媒体和 yt 上的伪类有关,但我不知道如何选择它们。

I'm trying to develop a site with some youtube videos. After I retrieve the XML file from their API, I have the following.

    <?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>

I'm trying to retrieve the length of this video from with PHP but with

echo $xml->media->yt

But it's not working. I think it has something to do with the psuedo class on media and yt but I don't know how to select those.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-11-22 08:37:29

尝试 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;

Try 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;
花之痕靓丽 2024-11-22 08:37:29

这些 XML 元素是有命名空间的。您需要获取名称空间信息

例子

// 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();

Those XML elements are namespaced. You need to get the namespace information.

Example

// 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();
肤浅与狂妄 2024-11-22 08:37:29

我假设您在这里使用 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'];

I'm assuming you're using SimpleXML here

$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'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文