Magpie RSS fetch PHP script not quite working, lil help?
Page 1 of 1
ZimZimma




Posts: 360

PostPosted: Tue, 17th Mar 2009 19:52    Post subject: Magpie RSS fetch PHP script not quite working, lil help?
I've been trying to get this script working, but there's one problem that I can't get past.

It's supposed to fetch the last 5 entries from the specified RSS feed and write the data to a text file (rss.txt). Unfortunately it seems as though each line overwrites the previous one, so only one entry is recorded instead of 5.

Here's the script.

Code:

<?php require_once('rss_fetch.inc'); ?>
<?php
$rss = fetch_rss('http://feeddit.com/topics/apple.atom');
$items = array_slice($rss->items, 0, 5);
foreach ($items as $item) {
        $href = $item['link'];
$out = fopen('rss.txt', 'w') or die('Cannot create output file');
fwrite($out, $href);
}
fclose($out);
;
?>


I think I need a "\n" in there somewhere, but I'm not sure and I've tried all that I can think of. Any ideas?
Back to top
lhzr




Posts: 3902
Location: RO
PostPosted: Tue, 17th Mar 2009 20:14    Post subject:
use fopen('rss.txt', 'w+') instead of fopen('rss.txt', 'w')
Back to top
lhzr




Posts: 3902
Location: RO
PostPosted: Tue, 17th Mar 2009 20:17    Post subject:
and open the file only once, if you do that you don't need to append instead of overwriting (w+ insteand just w):
Code:
<?php require_once('rss_fetch.inc'); ?>
<?php
$rss = fetch_rss('http://feeddit.com/topics/apple.atom');
$items = array_slice($rss->items, 0, 5);
$out = fopen('rss.txt', 'w') or die('Cannot create output file');
foreach ($items as $item) {
        $href = $item['link'];
fwrite($out, $href);
}
fclose($out);
?>


the original script kept reopening the file for every item and the file was opened in overwrite mode, deleting it's contents every time.
Back to top
ZimZimma




Posts: 360

PostPosted: Tue, 17th Mar 2009 20:56    Post subject:
Tnx man. I see what I did wrong, now.
Back to top
lhzr




Posts: 3902
Location: RO
PostPosted: Tue, 17th Mar 2009 21:09    Post subject:
i'm glad you saw the error of your ways, my son. go with god.

Razz
Back to top
ZimZimma




Posts: 360

PostPosted: Thu, 19th Mar 2009 05:51    Post subject:
*HALLELUJAH*
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group