0

I'm writing a PHP script to fetch rss new feed and show, using php simplexml_load_file i managed to show most of contents of feed,

but stuck with accessing image link.

My code is here

      $url = "https://www.nu.nl/rss/Algemeen";       
             $feeds = simplexml_load_file($url);
             foreach ($feeds->channel->item as $item) {

                        $title = $item->title;
                        $link = $item->link;
                        $description = $item->description;
                        $postDate = $item->pubDate;
//to get the image

$imageurl = $item->enclosure->url;

    //example output

    echo "<h3 class='rstitle'>$title</h3><div class='rscont'>$description<div>";

        }

But the image attribute inside another level

sample part of xml var_dump

object(SimpleXMLElement)#1 (2) {
  ["@attributes"]=>
  array(1) {
    ["version"]=>
    string(3) "2.0"
  }
  ["channel"]=>
  object(SimpleXMLElement)#2 (8) {
    ["title"]=>
    string(13) "NU - Algemeen"
    ["link"]=>
    string(26) "https://www.nu.nl/algemeen"
    ["description"]=>
    string(37) "Het laatste nieuws het eerst op NU.nl"
    ["language"]=>
    string(5) "nl-nl"
    ["copyright"]=>
    string(22) "Copyright (c) 2019, NU"
    ["lastBuildDate"]=>
    string(31) "Wed, 06 Feb 2019 10:41:00 +0100"
    ["ttl"]=>
    string(2) "60"
    ["item"]=>
    array(10) {
      [0]=>
      object(SimpleXMLElement)#3 (7) {
        ["title"]=>
        string(74) "747.000 Nederlanders in laatste vijf jaar slachtoffer van huiselijk geweld"
        ["link"]=>
        string(115) "https://www.nu.nl/binnenland/5726055/747000-nederlanders-in-laatste-vijf-jaar-slachtoffer-van-huiselijk-geweld.html"
        ["description"]=>
        string(216) "In totaal 747.000 volwassen Nederlanders zijn in de afgelopen vijf jaar minstens één keer slachtoffer geweest van huiselijk geweld. Daarnaast krijgen jaarlijks 90.000 tot 127.000 kinderen te maken met mishandeling."
        ["pubDate"]=>
        string(31) "Wed, 06 Feb 2019 10:21:36 +0100"
        ["guid"]=>
        string(28) "https://www.nu.nl/-/5726055/"
        ["enclosure"]=>
        object(SimpleXMLElement)#13 (1) {
          ["@attributes"]=>
          array(3) {
            ["url"]=>
            string(123) "https://media.nu.nl/m/m1nx2k0a3l3q_sqr256.jpg/747000-nederlanders-in-laatste-vijf-jaar-slachtoffer-van-huiselijk-geweld.jpg"
            ["length"]=>
            string(1) "0"
            ["type"]=>
            string(10) "image/jpeg"
          }
        }
        ["category"]=>
        array(2) {
          [0]=>
          string(8) "Algemeen"
          [1]=>
          string(10) "Binnenland"
        }
      }
      [1]=>
      object(SimpleXMLElement)#4 (7) {
        ["title"]=>
        string(68) "FvD en DENK willen in alle provincies meedoen aan Statenverkiezingen"
        ["link"]=>
        string(108) "https://www.nu.nl/algemeen/5726574/fvd-en-denk-willen-in-alle-provincies-meedoen-aan-statenverkiezingen.html"
        ["description"]=>
        string(206) "Forum voor Democratie (FvD) en DENK willen in alle twaalf provincies meedoen aan de Provinciale Statenverkiezingen op 20 maart. De Kiesraad heeft woensdag bekendgemaakt welke partijen zich hebben aangemeld."
        ["pubDate"]=>
        string(31) "Wed, 06 Feb 2019 10:41:00 +0100"
        ["guid"]=>
        string(28) "https://www.nu.nl/-/5726574/"
        ["enclosure"]=>
        object(SimpleXMLElement)#13 (1) {
          ["@attributes"]=>
          array(3) {
            ["url"]=>
            string(118) "https://media.nu.nl/m/k82xoojacd24_sqr256.jpg/fvd-en-denk-willen-in-alle-provincies-meedoen-aan-statenverkiezingen.jpg"
            ["length"]=>
            string(1) "0"
            ["type"]=>
            string(10) "image/jpeg"
          }
        }
        ["category"]=>
        array(2) {
          [0]=>
          string(8) "Algemeen"
          [1]=>
          string(8) "Politiek"
        }
      }
      [2]=>

I tried to access image url by $item->enclosure->url; but it returns empty array

can anyone help me to get the image url, Thanks a lot

1 Answer 1

2
$item->enclosure['url']

:)

Attributes are accessed by array accessor

You can also try xpath

$item->xpath('enclosure/@url')[0]

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, you saved my hours :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.