从 XML 中的 RSS 提要中提取图像

Extract Image from RSS Feed in XML

我正在尝试将我的 Wordpress 提要直接显示到我的外部 magento 站点中。我正在使用 feedburner 来实现这个结果。到目前为止,我已经能够显示博客标题 & link。为了改善外观,我还想在标题旁边显示博客。

这是我用来解析 XML Feed 的 php 代码,用于显示带有 link

的博客标题
<?php $channel = new Zend_Feed_Rss('http://feeds.feedburner.com/golfoy/pxce'); ?>
        <div class="block block-rss">

            <div class="block-content">
                <ol id="graybox-latest-news">

                    <?php foreach ($channel as $item): ?>
                    <li><a href="<?php echo $item->link; ?>"><?php echo $item->description; ?></a></li>
                    <?php endforeach; ?>
                </ol>
            </div>
        </div>

下面提到的是单个 Feed 项目的 XML 代码。您可以在 this URL

找到完整的提要
 <item>
    <title>The grey-haired major winners.</title>
    <link>http://blog.golfoy.com/the-grey-haired-major-winners/</link>
    <pubDate>Mon, 29 Aug 2016 08:24:53 +0000</pubDate>
    <guid isPermaLink="false">http://blog.golfoy.com/?p=980</guid>

    <description>
        <![CDATA[<img width="300" height="169" src="http://blog.golfoy.com/wp-content/uploads/2016/08/17_Jack_Nicklaus_20130805165206236_2560_1706.vresize.1200.675.high_.43-300x169.jpg" />
You are never too old to hit the greens and play a round of golf. In the history of men&#8217;s major championships, no golfer older than 48 (and just one golfer older than 46) has won. Have a look at the list of oldest men to have won a major: 1.Ben Hogan: The legend won [&#8230;]]]>
    </description>
    <content:encoded>
    ....
    </content:encoded>
    </item>

我正在尝试捕获整个 img 标签或只是 "src",它位于 Feed 项目的描述标签内。这样做的最佳做法是什么?

你select描述在xml 安德发送至 get_images .

step 1

包括simple_html_dom.php download

并更改函数中的定义(URL_IMAGE,PATH_UPLOAD)。

step 2

函数get_images($html)

require_once('simple_html_dom.php');

function get_images($html)
    {
        if (!empty($html))
        {

            $post_dom   = str_get_html($html);
            $first_img  = $post_dom->find('img');

            $image_list = array();
            foreach ($first_img as $image)
            {
                if(isset($image->src) AND $image->src != '')
                {
                    $image = $image->src;

                    $filetype = strtolower(substr(strrchr($image,'.'),1));
                    if (in_array($filetype,array('jpg','jpeg','png','gif')))
                    {
                        $filename = 'image_'.time().'_'.rand(0000000,99999999).'.'.$filetype;
                        $up = file_put_contents(PATH_UPLOAD.$filename,file_get_contents($image));
                        $size = filesize(PATH_UPLOAD.$filename);
                        list($width) = getimagesize(PATH_UPLOAD.$filename);

                        if(intval($size) >= 1024 AND $width >= 40)
                        {
                            $filename = URL_IMAGE.$filename;
                        }
                        else
                        {
                            unlink(PATH_UPLOAD.$filename);
                            $filename = '';
                        }

                        array_push($image_list, "$image|$filename");
                    }
                }
            }

            return $image_list;
        }
        else
        {
            return array();
        }
    }

step 3

select 描述

$html = $feed->item->description;
print_r(get_images($html));