-->

Can SimplePie grab images from feeds

2019-01-25 00:25发布

问题:

I am using this simplepie and i have uploaded the files to my host. Everything seems to be working fine except one thing. The blog that i'm getting the feed from has images in it, and when i use the simplepie to view the feed, the images don't show up. Is there a way to get the images to show up when i view the blog with simplepie?

Okay i am sorry, i am new at this. I am just using the code straight from the website to try and view the blog. i will put the code at the bottom here. So yea like i said i'm just trying to get the images to show up that are on the blog i'm reading from. Everything shows up great except that.

--- Header Information ---

<?php

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc.
require_once('simplepie.inc');

// We'll process this feed with all of the default options.
$feed = new SimplePie();

// Set the feed to process.
$feed->set_feed_url('http://wordpress.homickdesign.com/feed/');

// Run SimplePie.
$feed->init();

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code.  The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.
?>

--- Code ---

<h2><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_description(); ?></a><br />
      <span>Latest news from Robert Homick</span></h2>       

        <?php
$c=1;   
foreach ($feed->get_items() as $item):
if($c <= 3){
?>

        <p><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a><br />
        <?php echo $item->get_description(); ?><br />
        <span>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></span><br />
        <?php $c++;?>


<?php } endforeach;  ?>

回答1:

Yes, you can call get_content() and the entire post or the portion of the post that is included in the feed will be outputted to the screen, all the HTML tags included. If you want to pull say the first image in the post, you have to do a little more work, here is what I've been doing.

$htmlDOM = new simple_html_dom();
$htmlDOM->load($item->get_content());
$image = $htmlDOM->find('img', 0); 
$html .= '<img src="thumbnail.php?file=' . $image->src . '&maxw=300&maxh=300" border="0" />';

That snippet from my current little project, uses simple_html_dom and another script I got off the Internet to resize images on the fly. If you just want to display the image not resize it at all $image->src is the URL. I think functionality like this may be added to SimplePie in the future or an add-on to SimplePie, but in the mean time, it is four lines of code to display the first image from an item from a feed.



回答2:

This tutorial will help you to embed images in simple pie output.