I am coding a Tutorials site script with PHP,,,, i finished maximum of coding but i don't have any idea how to create a rss script,,, in my site there are different categories, so i need rss feed for all categories and also for individual categories,,,, do i need to create a separate table in database for RSS feed ???
You can get a nice tutorial for developing dynamic RSS feed for you site in PHP from the following website: http://net.tutsplus.com/tutorials/other/feeds-101/#more-7414
w3c has defined the structure for rss feed and atom feed. It will be a xml strict format. You can find that and create the one for you. By default it will have a title, author, date, and content tags. You will have to specify the namespace as well.
save the below code as rssfeed.php on the page which you need to be rss, add a link like this: http://yoursite.com/rssfeed.php?url=<? echo http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?> Now whenever somebody clicks the link, the page will be converted to a rss feed. <? $url= trim($_GET['url']; $result=@file_get_contents($url); $channel_title = ""; if (preg_match('/<title>(.*?)<\/title>/si', $result, $match) > 0) { $channel_title = $match[1];} if (preg_match('/<meta description(.*?)<\/description>"/si', $result, $match) > 0) { $channel_desc = $match[1];} if ($channel_desc == "") $channel_desc = $url; $title = wsstrip($channel_title); $result = preg_replace("'<style[^>]*>.*</style>'siU",'',$result); $result = preg_replace("'<head[^>]*>.*</head>'siU",'',$result); $str= strip_tags($result); $str2= explode("\n",$str); foreach($str2 as $key => $value){ $str2_len[$key] = strlen($value); } array_multisort($str2_len, SORT_DESC, SORT_NUMERIC, $str2); $description =trim(shorten($str2[0],100)); //$description=utf8_encode($description); /////////////////////////////////////////////// header("Content-Type: text/xml"); $output =""; $output .= "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $output .= "<!-- generator=\"RSSIT/2.0\" -->\n"; $output .= "<rss version=\"2.0\">\n"; $output .= "<channel>\n"; $output .= "<title>". htmlentities(strip_tags($link)) ."</title>\n"; $output .= "<link>". htmlentities($link) ."</link>\n"; $output .= "<description>The RSS feed of ". htmlentities(strip_tags($channel_title)) ." at ".$link." </description>\n"; $output .= "<webMaster>". htmlentities("artikels@mowspace.co.za") ."(Godfrey Philander)</webMaster>\n"; $output .= "<generator>". htmlentities("RSSIT") ."</generator>\n"; $output .= "<language>en</language>\n"; $output .= "<item>\n"; $output .= "<title>". htmlentities($title) ."</title>\n"; $output .= "<link>". htmlentities($url) ."</link>\n"; $output .= "<description>". htmlentities($description)."</description>\n"; $output .= "</item>\n"; $output .= "</channel>\n"; $output .= "</rss>\n"; print $output; //////////////////////////////////////////// function shorten($string,$maxWords,$end=NULL) { if (!isset($end)) { $end = ''; } $temp = explode(' ',$string); for ($i=0; $i<$maxWords; $i++) { $temp2[] = $temp[$i]; } $endResult = implode(' ',$temp2).$end; unset($temp,$temp2); return $endResult; } function wsstrip($str) { $str=ereg_replace("[\r\t\n]"," ",$str); $str=ereg_replace (' +', ' ', trim($str)); return $str; } ?> PHP:
Thank you very much, this is exactly what i need for my site But why the description is the same on every page? How I can get the content of the page to add to the description?
<? $url= trim($_GET['url']; $result=@file_get_contents($url); $channel_title = ""; if (preg_match('/<title>(.*?)<\/title>/si', $result, $match) > 0) { $channel_title = $match[1];} if (preg_match('/<META name=\"DESCRIPTION\" content=\"(.*?)\">/si', $result, $match) > 0) { $head_description = $match[1];} $title = wsstrip($channel_title); $result = preg_replace("'<head[^>]*>.*<\/head>'siU",'',$result); $str= strip_tags($result); $str2= explode("\n",$str); $str3= get_longest_value($str2); $description =trim(shorten($str3,100)); if(trim($description) ==""){$description=$head_description;} /////////////////////////////////////////////// header("Content-Type: text/xml"); $output =""; $output .= "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $output .= "<!-- generator=\"RSSIT/2.0\" -->\n"; $output .= "<rss version=\"2.0\">\n"; $output .= "<channel>\n"; $output .= "<title>". htmlentities(strip_tags($link)) ."</title>\n"; $output .= "<link>". htmlentities($link) ."</link>\n"; $output .= "<description>The RSS feed of ". htmlentities(strip_tags($channel_title)) ." at ".$link." </description>\n"; $output .= "<webMaster>". htmlentities("artikels@mowspace.co.za") ."(Godfrey Philander)</webMaster>\n"; $output .= "<generator>". htmlentities("RSSIT") ."</generator>\n"; $output .= "<language>en</language>\n"; $output .= "<item>\n"; $output .= "<title>". htmlentities($title) ."</title>\n"; $output .= "<link>". htmlentities($url) ."</link>\n"; $output .= "<description>". htmlentities($description)."</description>\n"; $output .= "</item>\n"; $output .= "</channel>\n"; $output .= "</rss>\n"; print $output; //////////////////////////////////////////// function get_longest_value($array) { // Some don't like to initialize, I do $longest = NULL; $longestLen = -1; foreach ($array as $value) { $len = strlen($value); if($len>$longestLen) { $longest = $value; $longestLen = $len; } } $longest = str_replace("\r\n", "\n", $longest); if (get_magic_quotes_gpc()) { return stripslashes($longest); } return $longest; } function shorten($string,$maxWords,$end=NULL) { if (!isset($end)) { $end = ''; } $temp = explode(' ',$string); for ($i=0; $i<$maxWords; $i++) { $temp2[] = $temp[$i]; } $endResult = implode(' ',$temp2).$end; unset($temp,$temp2); return $endResult; } function wsstrip($str) { $str=ereg_replace("[\r\t\n]"," ",$str); $str=ereg_replace (' +', ' ', trim($str)); return $str; } ?> PHP:
@ puppetguy For the meta scraping you could use -> get_meta_tags() You could also make use of php's SimpleXML