Hi, I've been teaching myself PHP and am trying to create a script that pulls the titles of an RSS feed then checks titles against a list of titles in a text file. If the title does not exist, it adds it to the file and prints it on the screen, otherwise it skips it. I use SimplePie for the RSS pulling. This is my code: <?php require_once("simplepie/simplepie.inc"); $feed = new SimplePie('http://www.sonicbids.com/RSS/Feeds/UpcomingSubmissionDeadlines.rss'); $file = fopen("sonicbids.txt", "a+"); $filearray = file($file); foreach($feed->get_items() as $item) { $flag = 0; foreach($filearray as $line) { if ($item->get_title() == $line) { $flag = 1; } } if ($flag==0) { fwrite ($file, $item->get_title()."\r\n"); echo $item->get_title(); echo ";"; ?> <br /> <?php } } ?> When I run it I get this error code: Warning: file() expects parameter 1 to be string, resource given in /blah/blah/b/l/a/blah/html/sonicbidsrss.php on line 9 Then for each item in the Feed it spits out this error: Warning: Invalid argument supplied for foreach() in /blah/blah/b/l/a/blah/html/sonicbidsrss.php on line 14 Any help is greatly appreciated, thank you.
SimplePie is not really needed for this, it's over kill. If you're using PHP 5 - you can use SimpleXML. Which would be a lot faster. This does what you want with no extra libraries: <?php $feed_url = 'http://www.sonicbids.com/RSS/Feeds/UpcomingSubmissionDeadlines.rss'; $feed_txt = 'sonicbids.txt'; // get the RSS feed $feed_xml = new SimpleXMLElement(file_get_contents($feed_url)); // retrieve all the current titles that are saved in the text file $titles = file($feed_txt, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); // create a file handle so we can write new ones into the file $file_handle = fopen($feed_txt, 'a+'); foreach ($feed_xml->channel->item as $item) { if (! in_array($item->title, $titles)) { fwrite($file_handle, $item->title . "\r\n"); echo $item->title . ';'; } } ?> PHP:
Wow, yeah that does seem a lot simpler. That in_array function is very useful, thank you. For the sake of learning what would you say was actually wrong with my script? Was I not pulling the file into an array correctly since it was giving me an error on that line? Thank you again.
You passed file() a file handle instead of a string (the filename). You should have also trimmed the newline from each line (you could use the FILE_IGNORE_NEW_LINES flag to do this automatically). Glad to help.