0

I want to get the URL from the enclosure tag with PHP

This is what i get from the RRS feed

<item>
    <title>Kettingbotsing met auto&#039;s en vrachtwagen op A2</title>
    <link>https://www.1limburg.nl/kettingbotsing-met-autos-en-vrachtwagen-op-a2</link>
    <description>&lt;p&gt;Drie auto&amp;#39;s en een vrachtauto zijn woensdagochtend met elkaar gebotst op de A2.&amp;nbsp;&amp;nbsp;&lt;/p&gt;</description>
    <pubDate>Wed, 21 Nov 2018 07:37:56 +0100</pubDate>
    <guid permalink="true">https://www.1limburg.nl/kettingbotsing-met-autos-en-vrachtwagen-op-a2</guid>
    <enclosure type="image/jpeg" url="https://www.1limburg.nl/sites/default/files/public/styles/api_preview/public/image_16_13.jpg?itok=qWaZAJ8v" />
 </item>

This is the code im using now

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml_string);

foreach ($xmlDoc->getElementsByTagName('item') as $node) {
    $item = array(
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'img' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue
    );
    echo "<pre>";
    var_dump($item);
    echo "</pre>";
}

And this is the result

array(2) {
    ["title"]=>
    string(46) "Kettingbotsing met auto's en vrachtwagen op A2"
    ["img"]=>
    string(10) "image/jpeg"
}

I'm currently getting the type of the enclosure tag, but i'm searching for the url.

Can someone help me, Thanks in advance

  • Possible duplicate of [Get enclosure img url from rss feed](https://stackoverflow.com/questions/19930692/get-enclosure-img-url-from-rss-feed) – tur1ng Nov 21 '18 at 09:35
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – hanshenrik Nov 21 '18 at 10:16

4 Answers4

2

As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...

$doc = simplexml_load_string($xml_string);
foreach ($doc->item as $node) {
    $item = array(
        'title' => (string)$node->title,
        'img' => (string)$node->enclosure['url']
    );
    echo "<pre>";
    var_dump($item);
    echo "</pre>";
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
1

You need to use getAttribute() instead of attributes property

$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
Mohammad
  • 21,175
  • 15
  • 55
  • 84
0

DOM supports Xpath expressions to fetch node lists and single values from an XML.

$document = new DOMDocument();
$document->loadXML($xml_string);
$xpath = new DOMXpath($document);

// iterate any item node in the document
foreach ($xpath->evaluate('//item') as $itemNode) {
    $item = [
        // first title child node cast to string
        'title' => $xpath->evaluate('string(title)', $itemNode),
        // first url attribute of an enclosure child node cast to string
        'img' => $xpath->evaluate('string(enclosure/@url)', $itemNode)
    ];
    echo "<pre>";
    var_dump($item);
    echo "</pre>";
}
ThW
  • 19,120
  • 3
  • 22
  • 44
0

something simple like

$node->enclosure['url']

should work i guess? At least with simplexml https://www.php.net/manual/en/book.simplexml.php

Mels_D
  • 109
  • 7