Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > php > mysql > rss

php > mysql > rss
Thread Tools
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Oct 5, 2004, 01:02 AM
 
I need to make a news page for a site, my plan is to make an input form that throws the "headline, description and article" into a Mysql database, then have a page that generates an xml rss feed from the database.

Then the latest news items will magically appear on the front page of the site, have all the editablity that Mysql offers and have the possibility to be read in a rss reader.

....but first post a thread here to make sure that there isn't some fatal flaw in my blueprint, or to check that there isn't some ready made technology available to acheive what I am setting out to do.
     
skalie  (op)
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Oct 5, 2004, 04:57 AM
 
Idea has been vetoed by my boss, he wants only xml / rss.

     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Oct 5, 2004, 11:36 AM
 
There are a lot of open source content management systems that do all of this for you, and are trivial to setup. A number of them are domeoes live over at OpenSourceCMS. I did a bunch of playing around, and finally decided on AngelineCMS.

All of the CMSs on the site are free, and would probably be faster to setup then trying to create your own system, even taking into account customizing time.
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 18, 2005, 02:28 PM
 
I need to learn how to change my dynamic .php news pages which are generating content from a mysql database into an RSS feed. Can the php pages themselves be a news feed? Or do I have to invent something that will go straight from mysql to a .rss file?

Anyone know how MacNN dynamicly generates their .rss file?
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Feb 18, 2005, 02:52 PM
 
There is nothing preventing PHP from generating RSS in exactly the same way that it generates HTML. The only caveat is that you need to send the right Content-Type header (I think it's "text/rss+xml") instead of text/html, which is what it sends by default.

Other than that, you do it the same way. You can generate other file formats too, as long as you send the right Content-Type header and make sure that you're actually outputting a valid file of that format. Some people even use PHP to generate raw images.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 18, 2005, 03:26 PM
 
Ok cool. So as long as my dynamic news .php suffixed pages have the right header, and related xml, rdf, or whatever it is tags, I can have people subscribe to a http://www.url.com/page.php URL? And will Firefox also see that it's an RSS enabled page and have its RSS icon appear in its status bar?
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Feb 18, 2005, 08:15 PM
 
Originally posted by darcybaston:
Ok cool. So as long as my dynamic news .php suffixed pages have the right header, and related xml, rdf, or whatever it is tags, I can have people subscribe to a http://www.url.com/page.php URL?
Yes, but the content-type header is important, since that's not the usual file extension.
And will Firefox also see that it's an RSS enabled page and have its RSS icon appear in its status bar?
Not in and of itself, because that's not how Firefox tells that a page is RSS enabled. To do that, you need to link to the RSS feed from an HTML page somewhere. There are two ways you can do this. One is to use a clickable link, but you'll need to use the type="text/rss+xml" attribute on the A tag (yes, this is valid HTML).

The other way is through a special link tag in your page's head section; this is the traditional way of doing it. The tag looks like this:
Code:
<link rel="alternate" type="application/rss+xml" title="RSS" href="[i]url-of-feed[/url]" />
This kind of link isn't actually visible on the page, but browsers and RSS readers will pick it up and recognize it for what it is.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 18, 2005, 10:47 PM
 
Oh wow, this is stellar help Millenium. Thank you so much.

Do you have any code handy for setting the header to the right kind? I'll certainly look it up if you refrain, but since I have your generosity for the moment...
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 19, 2005, 10:28 AM
 
Ok, I'm making progress. The feed validator I'm using (http://feedvalidator.org) is reporting 3 kinds of errors.

The first is
Your feed appears to be encoded as "ISO-8859-1", but your server is reporting "US-ASCII"
What does that mean? That I have to add one of those <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> lines?

Error #2 is because my links are dynamic and have an '=' character. The validator is rejecting these links:
... rg/forums/local_links.php?action=jump&id=389</link>
and has the error marker pointing to the first = character before the word jump.

Do I have to escape it or something?

Error #3 is some of my descriptions have things like '\x96' or '\x97' marked in red in their text here and there. Are those invisible characters its rejecting that I have to remove? I don't know what those could be, but maybe because my descriptions were cut and paste, some lf or cr chars bled through?
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 19, 2005, 10:48 AM
 
Ok, I'm learning to use $amp; (&) and &#61; (=) in my urls. Seems to please the validator. Now on to fixing other chars like dash flavors.
     
Grizzled Veteran
Join Date: Jun 2001
Status: Offline
Reply With Quote
Feb 19, 2005, 11:32 AM
 
It should be noted that if you are using PHP to generate the RSS per-request, it may cause a lot of activity on the server, however the benefit of having "live" information like that is that your readers will have up to the second accuracy then their news aggregator refreshes.

I usually have the content stored in MySQL, a handful of password protected (and session-based) pages to update the MySQL content, the website itself pull from the database, and a cron job that runs a private PHP script to generate the RSS file that your users will subscribe to. I have it set to run every 5 minutes, which ought to be good enough. Compare once every 5 minutes to getting the data from the DB and creating the RSS XML for each RSS request... If you have a lot of users requesting the RSS PHP script (and thus running a new instance of it for each request), your server will be doing a lot of unnecessary work.

And Millenium is right about the Content-Type.

Code:
function passToBrowser() { $this->buildRss(); header("Content-Type: text/xml"); print($this->xml); }
(In that script I sent you via e-mail, passToBrowser() changes the Content-Type in the HTTP header, making the browser or RSS reader to handle the content).
(Last edited by macgyvr64; Feb 19, 2005 at 11:39 AM. )
     
Grizzled Veteran
Join Date: Jun 2001
Status: Offline
Reply With Quote
Feb 19, 2005, 11:36 AM
 
Originally posted by darcybaston:
Error #2 is because my links are dynamic and have an '=' character. The validator is rejecting these links: and has the error marker pointing to the first = character before the word jump.
Do I have to escape it or something?
You might try doing a htmlentities($link) around the link before you give it to the web browser... it depends on how you're using it whether or not you'll need this.

Originally posted by darcybaston:
Error #3 is some of my descriptions have things like '\x96' or '\x97' marked in red in their text here and there. Are those invisible characters its rejecting that I have to remove? I don't know what those could be, but maybe because my descriptions were cut and paste, some lf or cr chars bled through?
That's exactly what happened. This got me for the longest time until I switched to BBEdit. If you open your script with BBEdit and click the third button from the left and choose "Show Invisibles", you'll surely see some dots •••••. Remove them and re-indent your code as needed, and you should be good on that front.
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Feb 19, 2005, 11:41 AM
 
Woohoo! Did it.

Thanks goes to Millenium, Collin, my Beginning PHP4 book by wrox, and the following URLs:

http://feedvalidator.org

http://jade.mcli.dist.maricopa.edu/f...ed2php.inc.txt

http://www.inr.net/tools/html-escape-codes.cfm

http://blogs.law.harvard.edu/tech/rss (good spec page)

And a little peeking at MacNN's own .rss file by loading it in my browser.

The end result is this feed: http://www.paxilprogress.org/paxilprogress_rss.php

I've tested it in both Firefox and NetNewsWire and it works brilliant. I'm proud. I went from knowing nothing about RSS, to doing it in two days.

The code for the file with sensitives removed is:
[php]
<?php

// Thanks for this Collin
header("Content-Type: text/xml");
//

// Thanks for this MacNN
echo "<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
<title>paxilprogress | Freedom is in you</title>
<link>http://www.paxilprogress.org/</link>
<description>The net's best source of information and support on Paxil and Paxil Withdrawal.</description>
<language>en-us</language>
<lastBuildDate>". date("r") . "</lastBuildDate>
<image>
<title>paxilprogress</title>
<url>http://www.paxilprogress.org/images/...gress_rss1.jpg</url>
<link>http://www.paxilprogresss.org</link>
</image>
";
//

// let's generate the links/site news! This is my own stuff.
$link_id = mysql_connect("ip", "loginname", "password");

$result=mysql_list_dbs($link_id);

mysql_select_db("database", $link_id);

$result=mysql_query("SELECT * FROM tbl_links WHERE linkmoderate='0' ORDER BY linkid DESC LIMIT 0,20", $link_id);

if($result) {
while($query_data=mysql_fetch_row($result)){

echo "<item>";
$cleanTitle=Htmlentities($query_data[1]);
$cleanTitle=strip_tags($cleanTitle);
$cleanTitle=str_replace("&nbsp;", " ", $cleanTitle);
echo "<title>" . "Link-" . $cleanTitle . "</title>";

// url with special entities since feed validator rejected the & and =
echo "<link>" . "http://www.paxilprogress.org/forums/local_links.php?action&#61;jump&amp;id& ;#61;$query_data[0]" . "</link>";

$cleanDescription=Htmlentities($query_data[2]);
$cleanDescription=strip_tags($cleanDescription);
$cleanDescription=str_replace("&nbsp;", " ", $cleanDescription);
$cleanDescription=str_replace("\x96", " ", $cleanDescription); //phantom character that gotta go
$cleanDescription=str_replace("\x97", " ", $cleanDescription); //another phantom
echo "<description>&quot;" . $cleanDescription . "</description>";

// I need to format the date. It's in int(10) format.
$rss_date=date('r', $query_data[8]);
//

echo "<pubDate>" . $rss_date . "</pubDate>";

echo "</item>
";
}
}

$result=mysql_query("SELECT * FROM tbl_sitenews WHERE newsType like 'Site News' ORDER BY newsID DESC LIMIT 0,15", $link_id);
if($result){
while ($query_data=mysql_fetch_row($result)){

echo "<item>";
$cleanTitle=Htmlentities($query_data[3]);
$cleanTitle=strip_tags($cleanTitle);
$cleanTitle=str_replace("&nbsp;", " ", $cleanTitle);
echo "<title>" . "Site News-" . $cleanTitle . "</title>";

echo "<link>" . "http://www.paxilprogress.org/newsDisplay.php?rid&#61;$query_data[0]" . "</link>";

$cleanDescription=Htmlentities($query_data[4]);
$cleanDescription=strip_tags($cleanDescription);
$cleanDescription=str_replace("&nbsp;", " ", $cleanDescription);
$cleanDescription=str_replace("\x96", " ", $cleanDescription);
$cleanDescription=str_replace("\x97", " ", $cleanDescription);
echo "<description>&quot;" . $cleanDescription . "</description>";

// From feed2php.inc.txt
// I need to format the date. It's in 0000-00-00 format.
$rss_date = date('r', strtotime($query_data[1]));
//

echo "<pubDate>" . $rss_date . "</pubDate>";

echo "</item>";
}
}

mysql_close($link_id);
?>

</channel>
</rss>[/php] The forum garbled my site's url field in the first part.
     
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
Feb 20, 2005, 03:14 AM
 
I was doing something like this for the first time a few weeks back and my biggest problem was that the DB had all kinds of wierd and wonderful characters in it.

On my searching I found:

function strictify ( $string ) {

$fixed = htmlspecialchars( $string, ENT_QUOTES );

$trans_array = array();
for ($i=127; $i<255; $i++) {
$trans_array[chr($i)] = "&#" . $i . ";";
}

$really_fixed = strtr($fixed, $trans_array);

return $really_fixed;

}
?>

at: http://uk2.php.net/chr

which I used like:

foreach($process_array[$index_key]['wrapper'] as $wrap_key=> $wrap_value){

echo "item processing: $index_key :: $wrap_key :: $wrap_value<br>\n";
echo $process_array[$index_key]['value'][$wrap_key] . "<br>\n";
$item .= '<' . $wrap_value . '>';
$item .= strictify($process_array[$index_key]['value'][$wrap_key]);
$item .= '</' . $wrap_value . ">\n";
}
$item .= "</item>";
$item_merge_array[] = $item;
unset($item);
}

I'm working with different sources so I built a function to which I can supply an array of values and change the XML by changing the values - combined with another function it can supply custom XML feeds for a variety of purposes.

Also if can change the ownership of your file/folder on the server to chmod 777 (i think), you can get php to write you a file ($somecontent is the XML output. Instead of echoing the output, you can place it all in a value, as above, by using '.=', I then placed this into an array so I had an entry for each item, this was subsequently imploded into a long string):

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

//echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);
//return true;

} else {
echo "The file $filename is not writable";
}



you can see my results here:

http://www.mobilegamefaqs.com/rssfeeds.php
(Last edited by moodymonster; Feb 20, 2005 at 04:17 AM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:13 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2