Topic: Creating RSS Feed with PHP? Pages that link to <a href="https://ozoneasylum.com/backlink?for=26850" title="Pages that link to Topic: Creating RSS Feed with PHP?" rel="nofollow" >Topic: Creating RSS Feed with PHP?\

 
Author Thread
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-14-2005 17:53 Edit Quote

I have been doing some searching on how to do this but I have only found stuff on parsing the XML document with PHP.

I was wondering how to just make the XML file to do the RSS feed. Or am I just missing something? <-- most likely the case. I have read through this article that was posted at Gurus but it has some stuff for ColdFusion.

Maybe I need to read some more on what RSS does?

Thanks in advance!

Later,

C:\

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

IP logged posted posted 10-14-2005 18:17 Edit Quote

RSS is simply an XML format so basically you just need to figure out the format and then get PHP to output the correct XML. You can do this as simple as just dumping XML just like you would HTML (which is probably the easiest method). When doing this you just want to send the xml header at the beginning (Not enitrely necessary but good for some browsers.

header('Content-type: text/xml')

Or if you're more familiar with the DOM from JS there are some built in DOM tools as well.


If you don't want to do all that work you can find a bunch of pre-built Libraries and Classes


Completely got here the wroing way but found this tute:
http://www.tiffanybbrown.com/articles/viewarticle.php/66

Personally if you're up form some learning I'd try to do this by hand at first just to get up to speed on using XML for feeds and stuff it's a good tool.



.:[ Never resist a perfect moment ]:.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-14-2005 18:34 Edit Quote

Yeah....I kind of knew that RSS feeds were just XML but couldn't really find anything on how to get the items to update or just pull in all the stuff from the site.

quote:

bitdamaged said:

Personally if you're up form some learning I'd try to do this by hand at first just to get up to speed on using XML for feeds and stuff it's a good tool.



I'm up for it I'd rather learn these things as opposed to using some built in tools. that last link looks like what I was looking for. On the drive to lunch I thought "Well why not just make a php page that queries the database and spit out the XML file..." Looks like what the tute describes.

Thanks for the quick response!

Later,

C:\

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 10-15-2005 02:56 Edit Quote

I, personally, would do this by hand too... such things can be outputted through PHP with ease anyway, so why not go the whole hog?

http://www.oreillynet.com/pub/a/network/2000/08/25/magazine/rss_tut.html

Then it would be a matter of using PHP loops and datbase calls, etc, to assemble an appropriate XML structure string, write it to a file, et voila, one RSS feed.


Justice 4 Pat Richard

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-16-2005 00:26 Edit Quote

wasn't that hard really to write out the XML file itself. Might do some expermenting with XSLT just for goofing off

quote:

Skaarjj said:

write it to a file,



are you saying to have the PHP write to a file? I was just going to have the PHP do the writing of the file.

Later,

C:\

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 10-16-2005 07:01 Edit Quote

PHP will do the writing. You'll use PHP to assemble the structure of the file, then write it out to another file for storage and to allow people to grab it and use it in their news aggregators.


Justice 4 Pat Richard

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-16-2005 17:59 Edit Quote

well I was just going to do it kind of like what sitepoint does.
It seems that the php page actually *is* the RSS feed. Just have the php page do as you say but when you put it into Sage
it works like an RSS feed.

Or am I just not getting this part?

Later,

C:\

Emperor
Maniac (V) Inmate

From: Cell 53, East Wing
Insane since: Jul 2001

IP logged posted posted 10-17-2005 15:43 Edit Quote

#cough#

http://www.gurusnetwork.com/discussion/thread/2960/

It does seem to be throwing errors (where it didn' used to) but that should be easily fixable.

___________________
Emps

The Emperor dot org | Revenant: The Zombie Magazine | Wonders | Justice for Pat Richard | FAQs: Emperor | Site Reviews | Reception Room

if I went 'round saying I was an Emperor just because some moistened bint had lobbed a scimitar at me, they'd put me away!

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

IP logged posted posted 10-17-2005 18:13 Edit Quote

It's actually fairly easy. I've done this, so anyone can! :-)

You're essentially just echoing the data back out. XML validators will go a LONG way, as well as a good RSS aggregator like NewzCrawler, which supports most (if not all) of the various features that can be put into a feed.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-17-2005 19:11 Edit Quote

groovy stuff. I actually have one that seems to be doing the job. Just need to do some more with the SQL statement and it should be good to go. I just had php spit out the file which works pretty well. Going to go ahead and do some XSLT stuff just to make it look purty.

thanks for the info.

Later,

C:\

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

IP logged posted posted 10-20-2005 20:43 Edit Quote

I wrote this one about 2 years ago to allow administrators to post news to our intranet site and produce an rss feed that could be used if they wanted to provide other departments or outside agencies with syndicated content.




code:
<?php



include("../openNclose.php");
/*------------contents of openNclose------------------------
<?php
$open="<?xml version='1.0'?>
<rss version='0.91'>
  <channel>";
$close=" 
     </item>
  </channel>
</rss> ";
?>
-----------------------------------------------------------*/

if (isset($_POST['submit'])){
    
    if ($_POST['title2']==""){
        echo"<script>alert('Pleases supply a Title');
        history.back();</script>";
        exit;
    }
    
    
   
    
    
    
    if ($_POST['blurb2']==""){
        echo"<script>alert('Pleases supply text for your news post');
        history.back();</script>";
        exit;
    } 
	$title="StateOfAlaska";
	$link="http://state.ak.us";
	$blurb=$_POST['blurb'];
	$title2=$_POST['title2'];
	$link2=$_POST['link2'];
	$blurb2=$_POST['blurb2'];
	
	$file="../rss/".$title.".rss";       //----------------------------------THIS WILL CHANGE FOR PRODUCTION-------------------------------------
        
	$openFile=fopen($file,'w');
	$replace="$open";
	
	$replace.="	<title>$title</title>
		<link>$link</link>
		<description>$blurb</description>
		<item>
		   <title>$title2</title>
		   <link>$link2</link>
		   <description>$blurb2</description>
		
	";
	$replace.="$close";
	
	
	if(fputs($openFile,$replace)){
		echo"<script>alert('Your Update has been processed.')</script>";
		fclose($openFile);
	} 
	else{
		echo"My bad..";
	}
    
	echo "<br /><br /><font size ='6' weight='900'>Webmasters:</font> ";
    echo "<a href='http://intra1.admin.state.ak.us/newIntra/rss/".$_POST['title'].".rss'>";
    echo  "right-click/save as</a> to download a copy of the xml/rss file if you would like to host a copy ";
    echo "on your server also.<br /><br />";
    	
    	
    $file1="../rss/".$title.".rss";        //----------------------------------THIS WILL CHANGE FOR PRODUCTION-------------------------------------
	
	$file2="../rss/main.rss";       //----------------------------------THIS WILL CHANGE FOR PRODUCTION-------------------------------------

         $myFile=file($file1);
         
	
	  
	
	
		
	$content;     
	
	for ( $i = 5; $i < count($myFile)-2; $i++ )
	{
		 $content.= $myFile[$i];
	}
	
	$newGet=file($file2);
	
	
	
	
	$final="";    
	
	for ( $i = 0; $i < count($newGet)-2; $i++ )
	{
		$final.=$newGet[$i];    
	}
	
	$final.="$content";
	$final.="</channel>
	</rss>";
	

	$openFile2=fopen($file2,'w')or die("<font color=red><br>no/open</font>");
	if (fputs($openFile2,$final)){
		echo "<font color=red><br></font>";
	}
	else{
		 echo "<font color=red><br>no file created</font>";
	}
	

	//----------------------------------------------Load the DB with who posted and when

	function loadDB(){
	global $REMOTE_USER;
	$mysql_id = mysql_connect('localhost', '******', '********');
		mysql_select_db('news',$mysql_id);

	
	
	$now= date("lFj,Y");
	$query="INSERT into newsposts VALUES('".$REMOTE_USER."','".$_POST['title2']."','".$now."')";
	mysql_query($query)or die(mysql_error());
	
	}
	
    loadDB();	
			
}




//--------------------------------------------------------------

class xItem {
  var $xTitle;
  var $xLink;
  var $xDescription;
}

// general vars
$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;

// ********* Start User-Defined Vars ************
// rss url goes here

$uFile="../my.rss";

// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "2";
// ********* End User-Defined Vars **************

function startElement($parser, $name, $attrs) {
  global $curTag;

  $curTag .= "^$name";

}

function endElement($parser, $name) {
  global $curTag;

  $caret_pos = strrpos($curTag,'^');

  $curTag = substr($curTag,0,$caret_pos);

}

function characterData($parser, $data) { global $curTag; // get the Channel information first
  global $sTitle, $sLink, $sDescription;  
  $titleKey = "^RSS^CHANNEL^TITLE";
  $linkKey = "^RSS^CHANNEL^LINK";
  $descKey = "^RSS^CHANNEL^DESCRIPTION";
  if ($curTag == $titleKey) {
    $sTitle = $data;
  }
  elseif ($curTag == $linkKey) {
    $sLink = $data;
  }
  elseif ($curTag == $descKey) {
    $sDescription = $data;
  }

  // now get the items 
  global $arItems, $itemCount;
  $itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
  $itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
  $itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";

  if ($curTag == $itemTitleKey) {
    // make new xItem    
    $arItems[$itemCount] = new xItem();     

    // set new item object's properties    
    $arItems[$itemCount]->xTitle = $data;
  }
  elseif ($curTag == $itemLinkKey) {
    $arItems[$itemCount]->xLink = $data;
  }
  elseif ($curTag == $itemDescKey) {
    $arItems[$itemCount]->xDescription = $data;
    // increment item counter
    $itemCount++;
  }
}

// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
  die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
  if (!xml_parse($xml_parser, $data, feof($fp))) {
    die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
  }
}
xml_parser_free($xml_parser);




?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Untitled</title>
	<meta name="generator" content="BBEdit 7.0" />
<script language="Javascript">
<!--



//-->
</script>
</head>
<body>

<h2>Post a news article to be displayed on the Intranet.</h2>
<hr />
<?php
for ($i=0;$i<count($arItems);$i++) {
  $txItem = $arItems[$i];
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($txItem->xLink); ?>"><?php echo($txItem->xTitle); ?></a></font>
<br>
<?php
if ($bDesc) {
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><?php echo ($txItem->xDescription); ?>
<br>
<?php
}
echo ("<br>");
}
?>
<hr /><br />
<form action="index.php" method="post"onsubmit="">
<!--
<input type="text"name="title" maxlength="100">your dept. 
<font color="red"><small>* this will not display on the intranet news page</small></font><br />

<input type="text"name="link" maxlength="100">URL for your dept's home page. 
<font color="red"><small>* this will not display on the intranet news page</small></font><br /><br />
-->
<input type="hidden" name="blurb" value="homepage" >

<input type="text"name="title2" maxlength="100">title of news article.<br />

<input type="text"name="link2" maxlength="100">news article URL.
<font color="red"><small>* if there is no webpage for this news item, leave this field blank.</small></font><br />
<br />
intro. text for article goes in the box below<br />

<textarea name="blurb2" rows="10" cols="40">

</textarea><br /><br />
<input type="submit" name="submit" value="post news ">
</form>

</body>
</html>




It's more than a little sloppy, but it gets the job done!

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

(Edited by norm on 10-20-2005 20:48)

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 10-20-2005 22:25 Edit Quote

wow...thanks for that norm. I'll have a look at that when I get back home. This RSS feed was for a forum so the posting was taken care of.

It was a bit more difficult to get the XSLT going than I at first thought. But now I think I have my head wrapped around it a bit better. However....now I am a little more confused on the "beauty" of using XML and XSLT for web stuff. It was more difficult to do this than just do good xhtml markup and css stuff.

eigh....I'm almost done with it and I've learned something in the process so all is not lost thanks to those wacky inmates in the asylum

Later,

C:\



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu