如何显示此XML Feed中的图像?

时间:2012-05-25 13:16:46

标签: php xml curl feed

希望有人可以帮忙解决这个问题。我正在使用以下脚本来显示我网站上的Feed中的数据。由于某种原因,我无法获取要显示的Feed中的图像。我注意到图像地址中有空格,我想知道这是不是问题。但是我已经更改了Feed并用'%20'替换了空格,希望它们会显示出来。仍然没有运气。有任何想法吗?

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

            ?>

2 个答案:

答案 0 :(得分:0)

<?php
error_reporting(E_ALL);

$data = file_get_contents("http://www.dpm.uk.com/feeds/rss/product-syndication.ashx?pgid=6&feed=opt1");
$xml = new SimpleXMLElement($data);
$counter = 0;
foreach ($xml->item as $item) {

    if ($counter < 20){
             echo "<div class=\"feed-item\">
                Title: " . $item->title . ", " . "
                Year:  " . $item->Year . "
        <br></div>";
    }
}
?>

测试它的工作原理

答案 1 :(得分:0)

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

<强>更新

显示图像:

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

要显示所有图像:

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