Reputation: 11
I have a blog on wordpress.com and I have created the rss feed page..by any chance I can display one recent post on my website that is not linked with wordpress? I haven't linked wordpress on my website, as it keeps slow down my main web..
I have tried the code below but still not work for my feed (http://yuchun6002hk.wordpress.com/feed)
$rss = new DOMDocument();
$rss->load('http://yuchun6002hk.wordpress.com/feed');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 1;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
Upvotes: 0
Views: 160
Reputation: 3316
The code you've posted is accurate and works correctly on my server.
So, there are several possible reasons that might cause your code to fail working.
First, make sure you're using PHP5 - older versions like PHP4 do not support DomDocument.
Also, try enabling the PHP setting that enables accessing URL object like files. In order to do that, open your server's php.ini file, and modify it to contain this setting:
allow_url_fopen = On
This will allow DomDocument::load() to fetch and load XML from a remote URL, treating it as if it was a local path.
Please, note that this setting has some security implications that you might want to have a look on - as discussed here: Should I allow 'allow_url_fopen' in PHP?
In case it still fails to work after that, you should make sure that your server has the LibXML and DOM extensions enabled, as they are required in order to use the DomDocument class.
In case you don't have control over the server settings or extensions, you might try using SimpleXML and its built-in function simplexml_load_file()
. This, of course, will require you to rewrite your code, as manipulating SimpleXML objects is different than working with DomDocument.
Upvotes: 1