I have a non-Drupal Website, and on that site I want to post the latest entry to a particular blog. I would like to figure out how to do this without enabling RSS for my entire Drupal site at this point.

<div id="blog">
Latest blog post: <?php [what do I put in here?] ?>
</div>

I could run an SQL query to pull it from the database, but the non-Drupal site is on a separate server. I think it would be simpler to call up the URL of the most recent node added under the blog. But, I don't know how to do that. Hence this message.

Thanks

mrweaver

Comments

yelvington’s picture

RSS is already "enabled" and is the simplest solution for what you describe. If the remote server is running PHP5, you'd need no more than half a dozen lines of code.

mattrweaver’s picture

<div>Latest blog post:
<?php $blog = 'http://www.website.com/?q=blog/4/feed';
$page = file_get_contents($blog);

	if($page){
	$blog[] = array ();
	'title' = t('Blog post title'),
	'body' = t('Post'),
	'path' = '?q=node/4/feed'
	);
	
echo $page['title'] . "<br />" . $page['body']; }?></div>

Thanks

Finished products are for decadent minds. -- Isaac Asimov

Finished products are for decadent minds. -- Isaac Asimov

yelvington’s picture

This isn't a Drupal issue, but rather a PHP question. If you have PHP5, use SimpleXML to parse the retrieved RSS data. The print_r() function will help you understand the structure of the XML object, or you can use http://us2.php.net/manual/en/function.simplexml-element-xpath.php to search for the parts you want.

http://www.onlamp.com/pub/a/php/2004/01/15/simplexml.html

If you are running PHP4, you can look at the PEAR add-on libraries for similar functionality.

mattrweaver’s picture

Okay, PHP-wise, my eyes are bigger than my stomach, if you catch my drift.

I have been over SimpleXML but the examples are all for an xml document. All I want to extract is the friggin' title of the latest blog post, and display it on a separate webpage. I have enabled Blopapi. How do I use simplexml with a URL as opposed to a file name?

mrweaver

Finished products are for decadent minds. -- Isaac Asimov

yelvington’s picture

I have been over SimpleXML but the examples are all for an xml document.

RSS is XML.

Here is a barebones "fetch rss and display all the titles" example.

$str = file_get_contents("http://drupal.org/node/feed");

foreach(simplexml_load_string($str)->channel->item as $item)
{
    print "<a href=\"$item->link\">$item->title</a><br />";
}

Note that this will generate a call to your Drupal site every time the remote page is viewed. A more robust version would have error checking and, ideally, cache the result.

mattrweaver’s picture

"RSS is XML."

I know, but I wasn't sure if it was different for URLs.

is "channel" something I'm supposed to provide, or should it read literally "channel"?

When I display the Web page, instead of a link with a blog entry title, the code is displayed:

channel->item as $item){print "link\">$item->title
} ?>

This segment:

"link\">$item->title

is hyperlinked, but the URL is a mess.

Finished products are for decadent minds. -- Isaac Asimov

Finished products are for decadent minds. -- Isaac Asimov

jason ruyle’s picture

I have several ways I need to use something like this, and wanted to know:

1. Limited number of items to show (lets say 5)

2. Show only one item (latest) with title and content

------------------------------
i do stuff

------------------------------
i do stuff