开发者

SimpleXML - "Node no longer exists"

开发者 https://www.devze.com 2022-12-24 02:23 出处:网络
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 re

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++;
}


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);
}


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消