duanchao5258 2014-12-29 09:22
浏览 51
已采纳

如何在PHP中使用卷曲方法从亚马逊Rss Feed获取图像路径

I want To Fetch Image From xml tag in php Curl Method.... I am trying to fetch images from amazon RSS feed ...here is the link http://www.amazon.co.uk/gp/rss/bestsellers/electronics/560834/ref=zg_bs_560834_rsslink

here is my php code for fetching Image Using Curl Method. But It's Not Working..so please help me on this

<?php 
$feed = "http://www.amazon.co.uk/gp/rss/bestsellers/electronics/560834/ref=zg_bs_560834_rsslink";
// Use cURL to fetch text
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rss = curl_exec($ch);
curl_close($ch);

// Manipulate string into object
$rss = simplexml_load_string($rss);
$siteTitle = $rss->channel->title;
$cnt = count($rss->channel->item);

for($i=0; $i<4; $i++)
{
    $url = $rss->channel->item[$i]->link;
    $title = $rss->channel->item[$i]->title;
    $desc = $rss->channel->item[$i]->description;
    echo '<h3><a href="'.$url.'">'.$title.'</a></h3>';
    $image = $rss->channel->item[$i]->description->img->attributes()->src;

    echo "Image Path : ".$image;
}
  • 写回答

1条回答 默认 最新

  • doujie4787 2014-12-30 05:08
    关注

    I do not see an image URL in the channel description. So no channel wide image to start with.

    <?xml version="1.0"?>
    <rss version="2.0">
      <channel>
        <title>Amazon.co.uk: Bestsellers in Electronics &gt; Camera &amp; Photo</title>
        <link>http://www.amazon.co.uk/Best-Sellers-Electronics-Camera-Photo/zgbs/electronics/560834/ref=pd_zg_rss_ts_ce_560834_c</link>
        <description><![CDATA[The most popular items in Camera & Photo (Updated hourly). Note: Product prices and availability were accurate at the time this feed was generated but are subject to change.]]></description>
        <pubDate>Tue, 30 Dec 2014 04:54:42 GMT</pubDate>
        <lastBuildDate>Tue, 30 Dec 2014 04:54:42 GMT</lastBuildDate>
        <ttl>60</ttl>
        <generator>Amazon Community RSS 2.0</generator>
        <language>en-gb</language>
        <copyright>Copyright 2014, Amazon.com</copyright>    
        <item>
          ...snip...
        </item>
      </channel>
    </rss>
    

    Now, the following is the first item at the time I fetched a copy of the RSS:

    <item>
      <title>#1: Samsung UHS-I CLASS 10 32GB SD Micro Card with Adapter</title>
      <guid isPermaLink="false">top-sellers_electronics_560834_B00J29BR3Y</guid>
      <link>http://www.amazon.co.uk/Samsung-UHS-I-CLASS-Micro-Adapter/dp/B00J29BR3Y/ref=pd_zg_rss_ts_ce_560834_1</link>
      <pubDate>Tue, 30 Dec 2014 04:54:42 GMT</pubDate>
      <description><![CDATA[<div style="float:left;">
        <a class="url" href="http://www.amazon.co.uk/Samsung-UHS-I-CLASS-Micro-Adapter/dp/B00J29BR3Y/ref=pd_zg_rss_ts_ce_560834_1"><img src="http://ecx.images-amazon.com/images/I/41PsVtmh0KL._SL160_.jpg" alt="Samsung UHSI" border="0" hspace="0" vspace="0" /></a>
        </div><span class="riRssTitle">
        <a href="http://www.amazon.co.uk/Samsung-UHS-I-CLASS-Micro-Adapter/dp/B00J29BR3Y/ref=pd_zg_rss_ts_ce_560834_1">Samsung UHS-I CLASS 10 32GB SD Micro Card with Adapter</a>
        </span> <br /><span class="riRssContributor">by Samsung</span> <br />
        <img src="http://g-ecx.images-amazon.com/images/G/02/x-locale/common/icons/uparrow_green_trans._V192561975_.gif" width="13" align="abstop" alt="Ranking has gone up in the past 24 hours" title="Ranking has gone up in the past 24 hours" height="11" border="0" />
        <font color="green"><strong></strong></font> 183 days in the top 100 <br />
        <img src="http://g-ecx.images-amazon.com/images/G/02/detail/stars-4-5._V192253866_.gif" width="64" height="12" border="0" style="margin: 0; padding: 0;"/>(1432)
        <br /><br /><a href="http://www.amazon.co.uk/Samsung-UHS-I-CLASS-Micro-Adapter/dp/B00J29BR3Y/ref=pd_zg_rss_ts_ce_560834_1">Buy new:
        </a> <strike>£24.99</strike>  <font color="#990000"><b>£13.72</b></font>
        <br /><a href="http://www.amazon.co.uk/gp/offer-listing/B00J29BR3Y/ref=pd_zg_rss_ts_ce_560834_1">26 used & new</a>
        from <span class="price">£9.15</span><br /><br />(Visit the
        <a href="http://www.amazon.co.uk/Best-Sellers-Electronics-Camera-Photo/zgbs/electronics/560834/ref=pd_zg_rss_ts_ce_560834_1">Bestsellers in Camera & Photo</a>
        list for authoritative information on this product's current rank.)]]>
      </description>
    </item>
    

    And again, no image in the RSS. You can see images in the description though. This means displaying the description would include an image (well several in this case).

    If you really want to get just and only an image, you could parse the HTML of the description and search for the <img> tags and select one which has a fair size for your final page. It looks like that's you are trying to do already...

    However, your code seems to expect the description to be parsed by the call to the XML parser:

    $rss = simplexml_load_string($rss);
    

    Not the case because it is defined inside a CDATA which means it appears as one large piece of text to the XML parser (which is the right way of doing things here).

    So you have to extract the description, parse that separately, then search for the img tag inside that sub-xml document (note though that inside the description it may be HTML rather than XML, that means it may not compile with an XML parser because some tags are not properly closed for XML. This version seems that it is XML compatible.)

    $description = $rss->channel->item[$i]->description;
    $desc_xml = simplexml_load_string($description);
    // then you have multiple IMG, I'm not too sure how you get the count, I would imagine like this:
    $max = count($desc_xml->img);
    for($j = 0; $j < $max; ++$j)
    {
       $url = $desc_xml->img[$j]->attributes()->src;
       ...
    }
    

    Something like that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?