From c500a3d9d88ec1bf4b80b8e6843acdcb3779119c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Rodrigo=20Cal=C3=B3gero?= Date: Tue, 6 Sep 2022 03:07:27 -0300 Subject: [PATCH 1/2] Added default user-agent (#16) Shopify doesn't accept a CURL without `CURLOPT_USERAGENT` option, it returns `403 Forbidden` instead. --- src/Feed.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Feed.php b/src/Feed.php index 8fcba58..3a59c27 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -218,6 +218,7 @@ private static function httpRequest($url, $user, $pass) curl_setopt($curl, CURLOPT_TIMEOUT, 20); curl_setopt($curl, CURLOPT_ENCODING, ''); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result + curl_setopt($curl, CURLOPT_USERAGENT, ''); if (!ini_get('open_basedir')) { curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // sometime is useful :) } From bee7cf32f7b6c8f89d5d3188d1a560f82a5bc229 Mon Sep 17 00:00:00 2001 From: Ken Brazier Date: Mon, 9 Dec 2024 21:25:19 -0700 Subject: [PATCH 2/2] Handle items at root, not under channel. Feeds like Slashdot have items outside the tags. This commit brings them inside the channel tags. This handles "rdf syntax", though sub-optimally; there are rdf tag links inside the channel tags that are ignored. --- src/Feed.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Feed.php b/src/Feed.php index 3a59c27..ac89f7d 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -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); @@ -90,7 +106,7 @@ private static function fromRss(SimpleXMLElement $xml) } } $feed = new self; - $feed->xml = $xml->channel; + $feed->xml = $channel; return $feed; }