Get the URL tag from enclosure XML












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'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










share|improve this question























  • Possible duplicate of Get enclosure img url from rss feed

    – JAAAY
    Nov 21 '18 at 9:35











  • Possible duplicate of How do you parse and process HTML/XML in PHP?

    – hanshenrik
    Nov 21 '18 at 10:16
















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'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










share|improve this question























  • Possible duplicate of Get enclosure img url from rss feed

    – JAAAY
    Nov 21 '18 at 9:35











  • Possible duplicate of How do you parse and process HTML/XML in PHP?

    – hanshenrik
    Nov 21 '18 at 10:16














0












0








0


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'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










share|improve this question














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'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







php xml curl dom






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 9:13









Stan van HeertumStan van Heertum

459




459













  • Possible duplicate of Get enclosure img url from rss feed

    – JAAAY
    Nov 21 '18 at 9:35











  • Possible duplicate of How do you parse and process HTML/XML in PHP?

    – hanshenrik
    Nov 21 '18 at 10:16



















  • Possible duplicate of Get enclosure img url from rss feed

    – JAAAY
    Nov 21 '18 at 9:35











  • Possible duplicate of How do you parse and process HTML/XML in PHP?

    – hanshenrik
    Nov 21 '18 at 10:16

















Possible duplicate of Get enclosure img url from rss feed

– JAAAY
Nov 21 '18 at 9:35





Possible duplicate of Get enclosure img url from rss feed

– JAAAY
Nov 21 '18 at 9:35













Possible duplicate of How do you parse and process HTML/XML in PHP?

– hanshenrik
Nov 21 '18 at 10:16





Possible duplicate of How do you parse and process HTML/XML in PHP?

– hanshenrik
Nov 21 '18 at 10:16












3 Answers
3






active

oldest

votes


















1














You need to use getAttribute() instead of attributes property



$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')





share|improve this answer
























  • Yes thats it, thank you!

    – Stan van Heertum
    Nov 21 '18 at 9:20











  • @StanvanHeertum Glad to help you

    – Mohammad
    Nov 21 '18 at 9:21



















0














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





share|improve this answer































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





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408637%2fget-the-url-tag-from-enclosure-xml%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need to use getAttribute() instead of attributes property



      $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')





      share|improve this answer
























      • Yes thats it, thank you!

        – Stan van Heertum
        Nov 21 '18 at 9:20











      • @StanvanHeertum Glad to help you

        – Mohammad
        Nov 21 '18 at 9:21
















      1














      You need to use getAttribute() instead of attributes property



      $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')





      share|improve this answer
























      • Yes thats it, thank you!

        – Stan van Heertum
        Nov 21 '18 at 9:20











      • @StanvanHeertum Glad to help you

        – Mohammad
        Nov 21 '18 at 9:21














      1












      1








      1







      You need to use getAttribute() instead of attributes property



      $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')





      share|improve this answer













      You need to use getAttribute() instead of attributes property



      $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 21 '18 at 9:18









      MohammadMohammad

      15.8k123664




      15.8k123664













      • Yes thats it, thank you!

        – Stan van Heertum
        Nov 21 '18 at 9:20











      • @StanvanHeertum Glad to help you

        – Mohammad
        Nov 21 '18 at 9:21



















      • Yes thats it, thank you!

        – Stan van Heertum
        Nov 21 '18 at 9:20











      • @StanvanHeertum Glad to help you

        – Mohammad
        Nov 21 '18 at 9:21

















      Yes thats it, thank you!

      – Stan van Heertum
      Nov 21 '18 at 9:20





      Yes thats it, thank you!

      – Stan van Heertum
      Nov 21 '18 at 9:20













      @StanvanHeertum Glad to help you

      – Mohammad
      Nov 21 '18 at 9:21





      @StanvanHeertum Glad to help you

      – Mohammad
      Nov 21 '18 at 9:21













      0














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





      share|improve this answer




























        0














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





        share|improve this answer


























          0












          0








          0







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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:18









          Nigel RenNigel Ren

          28k61934




          28k61934























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





              share|improve this answer




























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





                share|improve this answer


























                  0












                  0








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





                  share|improve this answer













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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 9:45









                  ThWThW

                  13.5k21633




                  13.5k21633






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408637%2fget-the-url-tag-from-enclosure-xml%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents