How to create RSS feed for my site

Discussion in 'PHP' started by gr8webseller, Jan 29, 2010.

  1. #1
    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 ??? :):)
     
    gr8webseller, Jan 29, 2010 IP
  2. swarg

    swarg Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you looked at framework.zend.com/manual/en/zend.feed.writer.html ?
     
    swarg, Jan 29, 2010 IP
  3. boucurod

    boucurod Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    learn the structure of xml and generate your data into this format
     
    boucurod, Jan 29, 2010 IP
  4. yoursanjay

    yoursanjay Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    yoursanjay, Jan 29, 2010 IP
  5. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    HivelocityDD, Jan 30, 2010 IP
  6. puppetguy

    puppetguy Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    puppetguy, Feb 3, 2010 IP
  7. noname0

    noname0 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    Last edited: Feb 3, 2010
    noname0, Feb 3, 2010 IP
  8. puppetguy

    puppetguy Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?
    
    $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, Feb 6, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    @ puppetguy

    For the meta scraping you could use -> get_meta_tags()

    You could also make use of php's SimpleXML
     
    danx10, Feb 6, 2010 IP