User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 397,843 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,537 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 894 | Replies: 1
Reply
Join Date: Oct 2006
Posts: 5
Reputation: tleisher is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tleisher tleisher is offline Offline
Newbie Poster

XML and PHP

  #1  
Jul 12th, 2007
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?
Last edited by tleisher : Jul 12th, 2007 at 6:15 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 660
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: XML and PHP

  #2  
Jul 13th, 2007
Originally Posted by tleisher View Post
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC