All pastes #926429 Raw Edit

SimplePie: Pulling comments

public php v1 · immutable
·published 2008-03-03 18:29 UTC
rendered paste body
<?php// Include the SimplePie library.require_once('../simplepie.inc');// Instantiate a new SimplePie object. We've got a Blogspot and a WordPress feed for this test.$feed = new SimplePie();$feed->set_feed_url('http://michellejoklomp.blogspot.com/feeds/posts/default'); // Blogspot//$feed->set_feed_url('http://simplepie.org/blog/feed/'); // WordPress$success = $feed->init();// Set the HTTP headers once, at the very top of the page.$feed->handle_content_type();// If processing was successful and we have data...if ($success && $feed->data){	// If the feed had an error, explode into a gigantic ball of flames!	if ($feed->error())	{		die($feed->error());	}	// Display the title of the feed.	echo '<h2><a href="' . $feed->get_permalink() . '">' . $feed->get_title() . '</a></h2>' . "\n";	// Loop through each of the feed's items.	foreach ($feed->get_items() as $item)	{		// Display the basic item data. For this example, we'll strip the tags from the description.		echo '<h3><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></h3>' . "\n";		echo '<p><small>' . $item->get_date() . '</small></p>' . "\n";		echo '<p>' . strip_tags($item->get_description()) . '</p>' . "\n";		// Try to pull the comment feed URL. Finding comments in feeds is vague and non-standardized, so we'll ONLY check the ones we know: WordPress and Blogspot.		if ($item->get_link(1, 'replies'))		{			// Set the Blogspot comment feed URL.			$comment_feed_url = $item->get_link(1, 'replies');		}		else		{			// Set the WordPress comment feed URL.			$comment_feed_url = $item->get_item_tags('', 'comments');			$comment_feed_url = str_replace('#comments', 'feed/', $comment_feed_url[0]['data']);		}		// Instantiate a new SimplePie object for the comments.		$comments = new SimplePie();		$comments->set_feed_url($comment_feed_url);		$comments_success = $comments->init();		// If processing was successful and we have data...		if ($comments_success && $comments->data)		{			echo '<strong>Comments:</strong>' . "\n";			echo '<ul>' . "\n";			// Loop through each of the comments.			foreach ($comments->get_items() as $citem)			{				echo '<li><strong>' . $citem->get_author()->get_name() . ':</strong> <a href="' . $citem->get_permalink() . '">' . $citem->get_title() . '</a> &mdash; <small>' . $citem->get_date() . '</small></li>' . "\n";			}			echo '</ul>' . "\n";		}		echo '<hr />' . "\n";	}}?>