Search code examples
phpxmlcurlfeed

How do I display images from this XML feed?


hoping someone can help on this. i'm using the following script to display data from a feed on my website. for some reason i can't get the images in the feed to display. i've noticed that the images addresses have a space in them and i'm wondering if this is the problem. however i have changed the feed and replaced the spaces with '%20' in the hope that they would then display. still no luck. any ideas?

            <?php

            # INSTANTIATE CURL.
            $curl = curl_init();

            # CURL SETTINGS.
            curl_setopt($curl, CURLOPT_URL, "http://www.dpm.uk.com/feeds/rss/product-syndication.ashx?pgid=6&feed=opt1");
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);

            # GRAB THE XML FILE.
            $xml = curl_exec($curl);

            curl_close($curl);

            # SET UP XML OBJECT.
            $xmlObj = simplexml_load_string( $xml );

            $tempCounter = 0;

            foreach ( $xmlObj->item as $item )
            {                    
                # DISPLAY ONLY 10 ITEMS.
                if ( $tempCounter < 20 )
                {
                   echo "
                   <div class=\"feed-item\">
                   Title: {$item -> title}
                   Year: {$item -> Year}
                   Colours: {$item -> Colours}
                   Size: {$item -> Size}
                   Available: {$item -> Available}
                   Image: {$item -> Images}
                <br>

                   </div>
                   ";

                }

                $tempCounter += 1;
            }

            ?>

Solution

  • $img = $item->Images->img[0]->attributes();
    
    echo "
      <div class=\"feed-item\">
        Title: {$item->title}
        Year: {$item->Year}
        Colours: {$item->Colours}
        Size: {$item->Size}
        Available: {$item->Available}
        Image: {$img['src']}
        <br>
      </div>
    ";
    

    Update:

    To display the image:

    echo "Image: <img src=\"{$img['src']}\" width=\"{$img['width']}\" height=\"{$img['height']}\" />";
    

    To diplay all images:

    foreach ($item->Images as $img) {
      $img = $img->attributes();
      echo "Image: <img src=\"{$img['src']}\" width=\"{$img['width']}\" height=\"{$img['height']}\" />";
    }