从rss feed中读取图像

时间:2011-12-07 08:47:55

标签: php rss-reader

我正在阅读并将rss xml Feed数据存储在我的数据库中,包含图片网址和图片,并显示在我的网站网址中。为此我创建了一个功能,并且运行良好。但在rss数据中,我发现了不同名称和不同大小的相同图像。像

      <item>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_mn.jpg" width="320" height="240" credit="Danny Moloshok/AP Photo"/>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_mc.jpg" width="100" height="75" credit="Danny Moloshok/AP Photo"/>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_mv.jpg" width="264" height="198" credit="Danny Moloshok/AP Photo"/>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_wl.jpg" width="200" height="112" credit="Danny Moloshok/AP Photo"/>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_main.jpg" width="413" height="310" credit="Danny Moloshok/AP Photo"/>
        <media:thumbnail url="http://a.abcnews.com/images/Entertainment/ap_tiger_woods_jp_111205_wmain.jpg" width="640" height="360" credit="Danny Moloshok/AP Photo"/>
       <media:keywords>1</media:keywords>
        <title><![CDATA[Change of Plans: Romney Revamps as Gingrich Closes]]></title>
        <link><![CDATA[http://feeds.abcnews.com/click.phdo?i=b21d3190f683c1e9dd83653255d5e446]]></link>
     </item>
        <item>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_mn.jpg" width="320" height="240" credit="ABC News"/>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_mc.jpg" width="100" height="75" credit="ABC News"/>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_mv.jpg" width="264" height="198" credit="ABC News"/>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_wl.jpg" width="200" height="112" credit="ABC News"/>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_main.jpg" width="413" height="310" credit="ABC News"/>
        <media:thumbnail url="http://a.abcnews.com/images/International/abc_rq170_unmanned_drone_ll_111205_wmain.jpg" width="640" height="360" credit="ABC News"/>
        <media:keywords>5</media:keywords>
        <title><![CDATA[Iran Missing Drone Was on CIA Mission]]></title>
        <link><![CDATA[http://feeds.abcnews.com/click.phdo?i=42a589abb9eb7a87c2dcaeaaeeb2e383]]></link>

以及更多...........

这些都是一个不同大小的项目块中的相同图像。

我的功能是拍摄照片:

   public  function getNytimes($site_config = array()){
       $xmlContent = self::checkXmlValidator($site_config['rss_link']);
       if($xmlContent === false){
           /*update url if getting any error*/
            mysql_query("UPDATE feed_settings SET feed_settings.status = '0', feed_settings.error_log='Time:".date('Y-m-d H:i:s')." Invalid url or resopnse time too more' WHERE feed_settings.rss_link='".$site_config['id']."'");
             return false;
         }
       $xml1 = new SimpleXMLElement($xmlContent);
       $child = $xml1->getDocNamespaces();
       $xml1->registerXPathNamespace('media', $child['media']);
       $image_arr = $xml1->xpath('//media:content');
       $xml = @simplexml_load_string($xmlContent,'SimpleXMLElement', LIBXML_NOCDATA);
       for($counter = 0; $counter < sizeof($image_arr); $counter++){
           $image = null;
            $image =  @$image_arr[$counter]->attributes()->url;
       $sql_query = "INSERT INTO news_feeds SET `image_url` = '".$image."',

但这次它在数据库中保存了6个相同的图像。 那么我能从一个区块中获取单个图像呢?

2 个答案:

答案 0 :(得分:0)

如果图像几何图形是固定的,只需使用指定的宽度和高度属性保存图像。

答案 1 :(得分:0)

取决于您想要保存的图片。

$image =  @$image_arr[0]->attributes()->url;

这将使用_mn endfix保存图片。您删除了脚本末尾附近的for循环。