Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用Curl方法从amazon Rss提要获取图像路径_Php_Curl_Rss - Fatal编程技术网

如何在PHP中使用Curl方法从amazon Rss提要获取图像路径

如何在PHP中使用Curl方法从amazon Rss提要获取图像路径,php,curl,rss,Php,Curl,Rss,我想从php Curl方法中的xml标记获取图像。。。。我正在尝试从amazon RSS提要获取图像…这里是链接 下面是我使用Curl方法获取图像的php代码。但它不起作用,所以请帮我 <?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

我想从php Curl方法中的xml标记获取图像。。。。我正在尝试从amazon RSS提要获取图像…这里是链接

下面是我使用Curl方法获取图像的php代码。但它不起作用,所以请帮我

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

我在频道描述中没有看到图像URL。因此,没有通道范围的图像开始

<?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>
情况并非如此,因为它是在CDATA中定义的,这意味着它在XML解析器中显示为一大块文本,这是正确的处理方法

因此,您必须提取描述,单独解析它,然后在子xml文档中搜索img标记。注意,在描述中,它可能是HTML而不是xml,这意味着它可能无法使用xml解析器编译,因为有些标记对于xml没有正确关闭。这个版本似乎与XML兼容


类似于此。

img->attributes->src在描述中不存在。
<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>
$rss = simplexml_load_string($rss);
$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;
   ...
}