Hi, I have two websites. I have an RSS feed from one that I want to host on the other, just the headlines and links back to the second website...

Right now I'm using SimpleXML and fopen/fclose to bring in the XML documents. But for some reason it's not working... here is the code I have now:

<?php
$url = 'http://www.mywebsite.com/rss.xml';
$rss_file = fopen($url, r);
$xml = simpleXML_load_file($url);
 
foreach ($xml->item as $item) {
echo $item->title . '<br />';
}
fclose($rss_file);
?>

The RSS feed right now is just:

<item>
<title>This is a title</title>
<description>This is a desctription...</description>
</item>

Any ideas? Or is there a better way to do this?

Hi, I have two websites. I have an RSS feed from one that I want to host on the other, just the headlines and links back to the second website...

Right now I'm using SimpleXML and fopen/fclose to bring in the XML documents. But for some reason it's not working... here is the code I have now:

<?php
$url = 'http://www.mywebsite.com/rss.xml';
$rss_file = fopen($url, r);
$xml = simpleXML_load_file($url);
 
foreach ($xml->item as $item) {
echo $item->title . '<br />';
}
fclose($rss_file);
?>

The RSS feed right now is just:

<item>
<title>This is a title</title>
<description>This is a desctription...</description>
</item>

Any ideas? Or is there a better way to do this?

You don't need the fopen() call.

Here's the correct code:

$url = 'http://example.com/rss.xml';
$xml = simpleXML_load_file($url);

echo '<pre>'.htmlentities(print_r($xml, 1)).'</pre>';
 
foreach ($xml->channel->item as $item) {
	echo $item->title . '<br />';
}

simpleXML_load_file() will retrieve the contents of the xml file and parse it into an Object.

This line:

echo '<pre>'.htmlentities(print_r($xml, 1)).'</pre>';

will show you the structure of the $xml returned by simpleXML_load_file($url);
Its the same as var_dump() but it's a bit more readable.

The items are in in the array: $xml->channel->item

--

It's probably easier if you use an RSS lib since it simplifies the structure of the xml object for you. If you simpleXML_load_file() on a different type of RSS or ATOM feed for instance, you'll get a different structure since the XML structure changes.
Good one to try is: magpie RSS.

Thank a lot that code help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.