简体   繁体   中英

Extract Image from RSS Feed in XML

I am trying to display my Wordpress feed directly into my external magento site. I am using feedburner to achieve this result. So far i have been able to display the blog title & link. To improve the look I also want to display the Blog right next to the title.

Here is the php code i am using to parse the XML Feed for displaying the blog titles with 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>

Below mentioned is the XML code for a single feed item. You can find the complete feed at 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>

I am trying to catch the entire img tag or just the "src" which is inside the description tag of a Feed Item. What would be the best practice to do this?

you select description in xml ande send to get_images .

step 1

include simple_html_dom.php download

and change define ( URL_IMAGE , PATH_UPLOAD ) in function .

step 2

function 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 description

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM