Jump to content

[SOLVED] Processing WordPress Feed in PHP?


Jamesking56

Recommended Posts

Hi,I'm trying to get my WordPress Blog to show in an Admin Panel Dashboard for my script. Its an XML.Whats the best way to do this?My Feed is: http://www.powercp.co.uk/feed/I want it so that it looks like:

<h3><a href="{ARTICLE LINK}">{ARTICLE TITLE}</a></h3><p>{DATE & TIME} by {ARTICLE CREATOR}</p>

How can I do this?(UPDATE: Solved!)

Link to comment
Share on other sites

You want to parse the feed externally?
Yeah, this script I'm making will be installed on other domains so I want to pull it down from that URL.How can I do this?
Link to comment
Share on other sites

Yeah, this script I'm making will be installed on other domains so I want to pull it down from that URL.How can I do this?
A couple ways.First you have to start by getting the feed data. The simplest way would be to use file_get_contents if you have allow_url_fopen on. Otherwise, you can cURL it.Once you actually have the data, the next step is to choose a method of parsing it. In my opinion, the easiest way would be to use SimpleXML (http://us2.php.net/manual/en/book.simplexml.php) and to create a simplexml object from the feed's markup. Then you can iterate through it and grab whatever data you need. Otherwise you could use regex's.I'm sure there are a lot of feed parser scripts out there already though. You can just use one of them rather than writing it yourself.
Link to comment
Share on other sites

I'm still stuck with this...Can someone show me the code for it?My code is currently:

	// Get Blog	$xmlDoc = new DOMDocument();	$xmlDoc->load("FEED URL HERE");		$x = $xmlDoc->documentElement;	foreach( $x->childNodes as $item )	{		//$powercp_blog .= $item->nodeName . " = " . $item->nodeValue . "<br />";		echo $item->nodeName . " | ";		if ( $item->nodeName == "channel" )		{			echo $item->nodeName;			$powercp_blog .= "<p>";			$powercp_blog .= $item->nodeValue;			$powercp_blog .= "</p>";		}	}		$template->assign('powercp_blog', $powercp_blog);

This displays it all stupidly (obviously).

Link to comment
Share on other sites

I'm still stuck with this...Can someone show me the code for it?This displays it all stupidly (obviously).
Again, I'd recommend SimpleXML over DOMDocument. Simple XML was made for this kind of thing. Here's an example of what it might look like:
$feed = new SimpleXMLElement('FEED_URL_HERE', 0, true);foreach ($feed->channel as $channel) {	echo('Channel Title:'.$channel->title);	echo('Channel Description:'.$channel->description);	foreach ($channel->item as $item) {		echo('Item Title: '.$item->title);		echo('Item Description: '.$item->description);	}}

Link to comment
Share on other sites

Again, I'd recommend SimpleXML over DOMDocument. Simple XML was made for this kind of thing. Here's an example of what it might look like:
$feed = new SimpleXMLElement('FEED_URL_HERE', 0, true);foreach ($feed->channel as $channel) {	echo('Channel Title:'.$channel->title);	echo('Channel Description:'.$channel->description);	foreach ($channel->item as $item) {		echo('Item Title: '.$item->title);		echo('Item Description: '.$item->description);	}}

This worked! Thanks guys its all working now and I've even limited it to 5 records and everything! Thank you! :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...