Asheesh Yadav Asheesh Yadav - 2 years ago 74
PHP Question

How to get Image path from amazon Rss Feed Using Curl Method in PHP

I want To Fetch Image From xml tag in php Curl Method.... I am trying to fetch images from amazon RSS feed is the link

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

$feed = "";
// 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);

// 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;

Answer Source

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">
    <title> Bestsellers in Electronics &gt; Camera &amp; Photo</title>
    <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>
    <generator>Amazon Community RSS 2.0</generator>
    <copyright>Copyright 2014,</copyright>    

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

  <title>#1: Samsung UHS-I CLASS 10 32GB SD Micro Card with Adapter</title>
  <guid isPermaLink="false">top-sellers_electronics_560834_B00J29BR3Y</guid>
  <pubDate>Tue, 30 Dec 2014 04:54:42 GMT</pubDate>
  <description><![CDATA[<div style="float:left;">
    <a class="url" href=""><img src="" alt="Samsung UHSI" border="0" hspace="0" vspace="0" /></a>
    </div><span class="riRssTitle">
    <a href="">Samsung UHS-I CLASS 10 32GB SD Micro Card with Adapter</a>
    </span> <br /><span class="riRssContributor">by Samsung</span> <br />
    <img src="" 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="" width="64" height="12" border="0" style="margin: 0; padding: 0;"/>(1432)
    <br /><br /><a href="">Buy new:
    </a> <strike>£24.99</strike>  <font color="#990000"><b>£13.72</b></font>
    <br /><a href="">26 used & new</a>
    from <span class="price">£9.15</span><br /><br />(Visit the
    <a href="">Bestsellers in Camera & Photo</a>
    list for authoritative information on this product's current rank.)]]>

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download