Jump to content

Risking to sound stupid - how do I automatically update feeds?


Perry Mason

Recommended Posts

I've been doing this manually. Basically, every time I add an entry to my site's mysql database I also manually edit the XML in the feed.xml file. I am sure there is an easier way to do that, however I couldn't find it. Not asking for a detailed tutorial, just a push in the right direction.

 

Thanks

Link to comment
Share on other sites

Make a feed.php that spits out an RSS feed according to whatever is in the database.

 

It's just like doing an HTML page except

1. You're outputting RSS XML, and

2. You need to tell the browser (or whatever) that the stuff isn't HTML (which PHP claims it is by default, thus confusing some browsers)

Link to comment
Share on other sites

  • 2 weeks later...

Here's a starter rss feed php code, change the values that you need, add whatever else would want.

 

This uses the same code and always seems to validate:

http://dynaindex.com/feed

 

http://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Fdynaindex.com%2Ffeed

 

<?php
    header("Content-Type: application/rss+xml; charset=UTF-8");

    DEFINE ('DB_USER', 'username');//change username
    DEFINE ('DB_PASSWORD', 'databasepassword');//change databasepassword
    DEFINE ('DB_HOST', 'localhost');//usually is localhost
    DEFINE ('DB_NAME', 'databasename');//change  databasename

    $rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
    $rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<atom:link href="http://mysite.com/feed" rel="self" type="application/rss+xml" />';//change
    $rssfeed .= '<title>Mysite.com Feed</title>';//change
    $rssfeed .= '<link>http://mysite.com</link>';//change
    $rssfeed .= '<description>New Posts</description>';//optional change
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Copyright (C) 2011 Mysite.com</copyright>';//change

    $connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
        or die('Could not connect to database');
    mysql_select_db(DB_NAME)
        or die ('Could not select database');

    $query = "SELECT * FROM table_name ORDER BY ID DESC LIMIT 0,10";//change table_name,maybe ID to id, optional mysql parameters
    $result = mysql_query($query) or die ("Could not execute query");

    while($row = mysql_fetch_array($result)) {
    
//change any of the values below to your specific needs   
$post_url = "http://".$row['post_title'];//change,this is for title
$hyperlink = "<a href='http://somesite.com/$post_url'>$post_url</a> ";//change
$title = $row['title_2'];//i use alternate titles and post_url is my titles
$title = htmlentities($title);//clean odd characters
$post_date = $row['post_date'];//date
$permalink = "http://somesite.com/".$row['post_name'];//change
$thumb = "http://somesite.com/images/imagelocation";//if added images change
//$rssfeed .= '<image>';
//$rssfeed .= '<url>' .$thumb. '</url>';
//$rssfeed .= '<title>' . $post_url . '</title>';
//$rssfeed .= '<link>' . $thumb . '</link>';
//$rssfeed .= '</image>';
        $rssfeed .= '<item>';
        $rssfeed .= '<title>' . $post_url . '</title>';
        $rssfeed .= '<guid>' . $permalink . '</guid>';
        $rssfeed .= '<description>' . $title . '</description>';
        //$rssfeed .= '<image>' .$thumb. '</image>';//may need to do images different
        $rssfeed .= '<link>' . $permalink .'</link>';
        $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($post_date)) . '</pubDate>';
        $rssfeed .= '</item>';
    }

    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';

    echo $rssfeed;
?>

 

Forgot to mention, just make a folder named feed, and name this file index.php

your address would then be like http://mysite.com/feed

Link to comment
Share on other sites

You might also want to take a look at using the zend framework for this.  One reason is that a single block of code will allow you to generate both rss and atom format feeds.  Here's code from a working feed that shows you everything you would need to know.  I've pulled out various specific queries and things, but the skeleton could be easily used to implement your feeds. 

 

 

/*
* 
* Feeds.php
* Parameters:  name= name_of_feed (feeda | feedb | etc)
* 			   format=atom | rss
*/
require_once('Zend/Feed/Writer/Feed.php');

function addEntry($writer, $entryData, $entryDescription) {
$entry = $writer->createEntry();
$entry->setTitle($entryData['title']); 
$entry->setLink(SERVER_URL . 'link to the full item id=' . $entryData['some_id'] . '&action=view'); 
// must supply a non-empty value for description
$content = !empty($entryData['content']) ? $entryData['content'] : "N/A";
$entry->setDescription($content);   
$entry->setDateCreated(time()); 
$entry->setDateModified(time()); 
$entry->addAuthor($entryData['author'], $entryData['email']); 
$writer->addEntry($entry); // manual addition post-creation 
}

function loadFeed($writer, $query, $entryDescription) {
   // your query here then loop through results calling addEntry and passing data
if ($result) {
	foreach($result as $row) {
		addEntry($writer, $row, $entryDescription);
	}
}
}

$format = isset($_REQUEST['format']) ? $_REQUEST['format'] : 'rss';
if (!($format == 'atom' || $format == 'rss')) {
$format = 'rss';
} 
$name = 'something';  // get from request or $_GET
// get the name of the specific feed, in case you have multiple different feeds you want to generate.  This is a junction box where you can use this one feed script
// to  generate the feeds for different sources of data in your site.
// Connect to db

Your db connection stuff needs to be done here in someway, or files read or whatever the data source is for the feed

switch ($name) {
case 'feeda' :
	$name = 'feeda';
	$title = 'this is feeda including the latest feeda entries!';
	$description = 'Feeda description here... blah blah';
	$entryDescription = 'this is a feedA item';
	$query = "SELECT a, b, c etc.  
				FROM sometable etc.";
	break;
case 'feedb' : 
default :
	$name = 'feedb';
                // etc.
	";
}

$url = 'http:.. yoursite/';
$link = $url . "feed.php?name=$name&format=$format";
$writer = new Zend_Feed_Writer_Feed; 
$writer->setTitle($title); 
$writer->setLink(SERVER_URL); 
$writer->setFeedLink(SERVER_URL . $link, $format);
$writer->setDescription($description);
$writer->setDateModified(time()); //Atom needs this  
loadFeed($writer, $query, $entryDescription);
echo $writer->export($format); 

Link to comment
Share on other sites

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.