【问题标题】:How do I display images from this XML feed?如何显示来自此 XML 提要的图像?
【发布时间】:2012-06-01 01:51:45
【问题描述】:

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

            ?>

【问题讨论】:

标签: php xml curl feed


【解决方案1】:
$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']}\" />";
}

【讨论】:

  • 感谢您的评论。使用上面的代码,我设法显示了图像的 src。所以提要显示如下:Title: Rapida 105-10 SW (10+0/5+5)Year: 2008Colours: 10 ColourSize: 740x1050mmAvailable: ImmediatelyImage: http://www.dpm.uk.com/images/May 12/2008_KBA_105_a.jpg 如何将其添加到一些标记中以便图像显示? XML 提要的 标记中还包含 3-4 张图像。我也可以拿这些而不是第一个吗?
【解决方案2】:
<?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>";
    }
}
?>

测试过

【讨论】:

  • 感谢您的评论。不幸的是,这似乎不起作用。当我将您的更改添加到代码中时,不会显示提要信息。有任何想法吗?我应该使用脚本顶部的任何“Curl”代码吗?有更好的方法吗?再次感谢。
  • 你可以试试 $xml = file_get_contents($your_link) 它应该可以工作
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2020-08-25
  • 2011-10-03
  • 1970-01-01
相关资源
最近更新 更多