-2

希望有人可以帮助解决这个问题。我正在使用以下脚本在我的网站上显示来自提要的数据。由于某种原因,我无法显示提要中的图像。我注意到图像地址中有一个空格,我想知道这是否是问题所在。但是我已经更改了提要并用“%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;
            }

            ?>
4

2 回答 2

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

测试它有效

于 2012-05-25T13:34:08.743 回答
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']}\" />";
}
于 2012-05-25T13:41:10.277 回答