Jump to content

[SOLVED] Processing WordPress Feed in PHP?


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!)

Edited by jamesking56
Link to post
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 post
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. Edited by Dilated
Link to post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...