Jump to content

Recommended Posts

ok so I am working on an RSS feed, but I have some questions on some things that I am not sure how to go about it.

 

right now I have

 

function getRSS($rss_url)
{
$content = file_get_contents($rss_url);
$xml = new SimpleXmlElement($content);

foreach($xml->channel->item as $entry)
{
	echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></br>";
}
}

 

This displays the RSS from a URL, but I want to be able use a form to post the link which will go to a text file.

 

This is the function I have for making the links go to the text file

 

function createRSS()
{
$filename = "rss.txt";
$text = $_POST['rssURL'];
$fp = fopen($filename, "a+");

if (!$fp) 
{
	echo ('File was not written');
} else {
	fwrite ($fp, $text."\n");
	fclose ($fp);

	echo 'File written </br>';
}
}

 

With this function, it creates the text file and inputs w/e I put in the text field, but I want to be able to retrieve each link separately to be able to display the RSS, and I want to be able to delete an RSS if I want to.

 

Could anyone point me into the right direction?

 

Thanks!

Im not 100% sure what you are trying to do here, but if the RSS urls are stored in a text file and each url is on a seperate line, you could do this:

 

<?php
$rss_urls = file('rss.txt');
foreach ($rss_urls as $url) {
    getRSS($url);
}
?>

Here is some more to get you started.

 

<?php
function getRSS($rss_url)
{
 echo '<h1>' . $rss_url . '</h1>';
   $content = file_get_contents(trim($rss_url));
 if ($content) {
   $xml = new SimpleXmlElement($content);
   foreach($xml->item as $entry)
   {
      echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></br>";
   }
 }
}

function createRSS($url)
{
   $filename = "rss.txt";
   $fp = fopen($filename, "a+");
   
   if (!$fp)
   {
      echo ('File was not written');
   } else {
      fwrite ($fp, $url."\n");
      fclose ($fp);
      
      echo 'File written </br>';
   }
}

if ($_POST['action'] == 'Add URL') {
createRSS($_POST['url']);
}


$rss_urls = file('rss.txt');


foreach ($rss_urls as $url) {
getRSS($url);
}

?>
<form method="post" action="">
<input type="text" name="url" /><br />
<input type="submit" name="action" value="Add URL" />
</form>

Hmm, I have the following code working on my server (http://losingallhope.com/rss/):

 

<?php
function getRSS($rss_url) {
echo '<h1>' . $rss_url . '</h1>';
$content = file_get_contents(trim($rss_url));
if ($content) {
	$xml = new SimpleXmlElement($content);
	if (count($xml->channel->item)) {
		foreach($xml->channel->item as $entry) {
			echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></br>";
		}
	} else if (count($xml->item)) {
		foreach($xml->item as $entry) {
			echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></br>";
		}
	}
}
}

function createRSS($url) {
   $filename = "rss.txt";
   $fp = fopen($filename, "a+");
   
   if (!$fp) {
      echo ('File was not written');
   } else {
      fwrite ($fp, $url."\n");
      fclose ($fp);
      
      echo 'File written </br>';
   }
}

if ($_POST['action'] == 'Add URL') {
createRSS($_POST['url']);
}

$rss_urls = file('rss.txt');
foreach ($rss_urls as $url) {
getRSS($url);
}

?>
<form method="post" action="">
<input type="text" name="url" /><br />
<input type="submit" name="action" value="Add URL" />
</form>

 

As far as removing urls from the text file, im going to leave that exercise for you. ;)

 

This kind of thing is best left to a database anywayz.

 

As far as removing urls from the text file, im going to leave that exercise for you. ;)

 

This kind of thing is best left to a database anywayz.

 

Thanks for the help, the script isn't the most user friendly. I've found out it will only work with http://FEEDURL

 

but most feed links are feed://FEEDURL

 

Thank for all the help. I appreciate it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.