Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle items at root, not under channel #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,23 @@ private static function fromRss(SimpleXMLElement $xml)

self::adjustNamespaces($xml);

foreach ($xml->channel->item as $item) {
$channel = $xml->channel;

if(isset($xml->item)) {
// Some feeds have items at root, not under channel. Add them under channel.
// Basic procedure from https://stackoverflow.com/questions/5735857/php-domdocument-move-nodes-from-a-document-to-another
$xml_dom = dom_import_simplexml($channel);
while(count($xml->item) > 0) {
// It makes no sense, but this has the side effect of popping the item as off a stack.
$item = $xml->item[0];
$item_dom = dom_import_simplexml($item);
$item_dom_adopted = $xml_dom->ownerDocument->importNode($item_dom, true);
$xml_dom->appendChild($item_dom_adopted);
}
$channel = simplexml_import_dom($xml_dom);
}

foreach ($channel->item as $item) {
// converts namespaces to dotted tags
self::adjustNamespaces($item);

Expand All @@ -90,7 +106,7 @@ private static function fromRss(SimpleXMLElement $xml)
}
}
$feed = new self;
$feed->xml = $xml->channel;
$feed->xml = $channel;
return $feed;
}

Expand Down