SimpleXML - “节点不再存在”

发布于 2024-08-26 10:01:47 字数 915 浏览 5 评论 0原文

我试图从这个 YouTube 播放列表提要中获取视频数据,并将有趣的数据添加到数组中并稍后使用,但正如您从提要中看到的,某些视频链接“已失效”,这会导致我的代码出现问题。

当我尝试访问 $attrs['url'] 时,收到的错误是“节点不再存在”。我已经尝试了几个小时来找到一种方法来在访问节点之前检查该节点是否存在,但我没有运气。

如果有人可以帮助我以其他方式解析提要并获得相同的结果,或者创建一个有效的 if-node-exists 检查,我将非常高兴。先感谢您

$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';

$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;

foreach ($sxml->entry as $entry) {
    // 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();
    $videoobj[$i]['url'] = $attrs['url'];

    // get video thumbnail
    $attrs = $media->group->thumbnail[0]->attributes();
    $videoobj[$i]['thumb'] = $attrs['url']; 
    $videoobj[$i]['title'] = $media->group->title;
    $i++;
}

I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.

The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.

If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance

$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';

$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;

foreach ($sxml->entry as $entry) {
    // 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();
    $videoobj[$i]['url'] = $attrs['url'];

    // get video thumbnail
    $attrs = $media->group->thumbnail[0]->attributes();
    $videoobj[$i]['thumb'] = $attrs['url']; 
    $videoobj[$i]['title'] = $media->group->title;
    $i++;
}

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

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

发布评论

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

评论(2

愿与i 2024-09-02 10:01:47
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
    $attrs = $media->group->thumbnail[0]->attributes();
    $videoobj[$i]['thumb'] = strval($attrs['url']);
    $videoobj[$i]['title'] = strval($media->group->title);
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
    $attrs = $media->group->thumbnail[0]->attributes();
    $videoobj[$i]['thumb'] = strval($attrs['url']);
    $videoobj[$i]['title'] = strval($media->group->title);
}
不再让梦枯萎 2024-09-02 10:01:47

SimpleXML 的方法总是返回对象,这些对象本身链接到原始文档(一些与 libxml 相关的内部事物)。如果要存储该数据以供以后使用,请将其转换为字符串,如下所示:

$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url']; 
$videoobj[$i]['title'] = (string) $media->group->title;

SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:

$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url']; 
$videoobj[$i]['title'] = (string) $media->group->title;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文