PDA

View Full Version : Php & rss


tux
05-31-2013, 09:41 PM
Hello all,

Its been ages, how the hell is everybody. Been out of the coding game for a couple of years so am well rusty.

I have taken on a rebuild website for my local camera club. Mostly okay with all of it. I suppose its like riding a bike. You never really forget!

One thing I am well behind on is RSS.

Can anyone point me in the right direction in setting up an RSS feed. I want to include feeds from a few photography websites. I have found a few sites that do the donkey work for you which is fine but I would really like to know how to do it myself.

Can anyone point me in the right direction please on how to set it up XML etc. using PHP.

Thanks, Paul

edbr
06-01-2013, 01:32 AM
hi paul. if you want to use php then basically you musy write the headers for xml commonly you use feeds from a database . If you want to include feeds from other sites to your page then jquery has some feed readers but there are some php scripts around but ive never had cause to use them

feed from a database mysql

echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<rss version="2.0">';
echo '<channel>';
echo '<title>Concord Services Bali RSS Feed</title>';
echo '<link>http://www.concordservices-bali.com</link>';
echo '<description>This RSS feed of the latest jobs posted .</description>';

require_once('../Connections/test.php');
mysql_select_db($database_test, $test);
$query_Recordset1 ="SELECT field1,field2,field3,field4,UNIX_TIMESTAMP(datenow ) AS datenow
FROM table ORDER by field1 DESC LIMIT 0,15";

then echo the results



while($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
echo '<item>'; // Begin a news article
echo '<title>' ."Feed title".$row_Recordset1['field1'].'</title>';
echo '<link>'."http://www.domain_name.com/page.php?recordID=".$row_Recordset1['field1'].'</link>';
echo '<description>'.htmlentities(strip_tags($row_Recordset1['field1'])) . "in " .strip_tags($row_Recordset1[field1'])."Salary ".htmlentities( strip_tags($row_Recordset1['field1'])). '</description>';

echo '<datenow>'.date('D, d M Y H:i:s',$row_Recordset1['datenow']).'</datenow>';
echo '</item>';
}

echo '</channel>';
echo '</rss>';

tux
06-01-2013, 02:16 PM
Thanks Edbr,

I have done a bit of research and have come up with this for anyone who may find it useful.

<?php
$rss = new DOMDocument();
$rss->load('http://your_rss_feed_URL_here/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>

Just put your rss feed link into the code. You will have to style it yourself of course. You can also limit the amount of feeds...

$limit = 5; //change to suit your needs//

I have actually put this into a function to keep it cleaner and added..

substr($description, 0, 100)

... to restrict the ammount of characters in the description.

Hope it helps someone.

Regards, Paul