1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

RSS Parser - PHP Script

Discussion in 'PHP' started by dcristo, Jan 1, 2006.

  1. #1
    I am looking for a off the shelf or custom made RSS parser which performs the same function as scripts like carp or magpie.

    These are great little scripts, but ideally I am wanting a solution which doesnt require you link out to the rss parser script development URL.
     
    dcristo, Jan 1, 2006 IP
  2. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Shows yahoo news for a given keyword .
    
    <?php
    $keyword="seo";
    $nr_news=6;
    echo '<p><b><font face="Arial" size="3" color="#3366CC">SEO News</font></b></p>';
    
    
    $file = 'http://news.search.yahoo.com/news/rss?p='.urlencode($keyword).'&ei=UTF-8&n=10&fl=0&sday=6&eday=5';
    
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;
    
    function startElement($parser, $name, $attrs) {
       	global $rss_channel, $currently_writing, $main;
       	switch($name) {
       		case "RSS":
       		case "RDF:RDF":
       		case "ITEMS":
       			$currently_writing = "";
       			break;
       		case "CHANNEL":
       			$main = "CHANNEL";
       			break;
       		case "IMAGE":
       			$main = "IMAGE";
       			$rss_channel["IMAGE"] = array();
       			break;
       		case "ITEM":
       			$main = "ITEMS";
       			break;
       		default:
       			$currently_writing = $name;
       			break;
       	}
    }
    
    function endElement($parser, $name) {
       	global $rss_channel, $currently_writing, $item_counter;
       	$currently_writing = "";
       	if ($name == "ITEM") {
       		$item_counter++;
       	}
    }
    
    function characterData($parser, $data) {
    	global $rss_channel, $currently_writing, $main, $item_counter;
    	if ($currently_writing != "") {
    		switch($main) {
    			case "ITEMS":
    				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
    					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
    				} else {
    					//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
    					$rss_channel[$main][$item_counter][$currently_writing] = $data;
    				}
    				break;
    		}
    	}
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    /*
    if (!($fp = fopen($file, "r"))) {
    	die("could not open XML input");
    }
    
    /*
    while ($data = fread($fp, 4096)) {
    	if (!xml_parse($xml_parser, $data, feof($fp))) {
    		die(sprintf("XML error: %s at line %d",
    					xml_error_string(xml_get_error_code($xml_parser)),
    					xml_get_current_line_number($xml_parser)));
    	}
    }
    */
    $data=curl_string($file);
    xml_parse($xml_parser,$data);
    xml_parser_free($xml_parser);
    
    // putting in array
    $news=array();
    if (isset($rss_channel["ITEMS"])) 
    {
    	if (count($rss_channel["ITEMS"]) > 0) 
    		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) $news[]=$rss_channel["ITEMS"][$i];
    }
    $c=0;
    foreach($news as $key=>$val)
    {
    	if($c<$nr_news)
    	{
    		echo "<p align=left>";
    		echo '<a href="'.$val['LINK'].'">'.$val['TITLE'].'</a> - '.$val['PUBDATE'].'<br>'.''.$val['DESCRIPTION'].'</font></p>';
    	}
    	$c++;
    }
    
    
    function curl_string ($url,$user_agent='Mozilla 4.0'){
    
           $ch = curl_init();
    
           curl_setopt ($ch, CURLOPT_URL, $url);
           curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
           curl_setopt ($ch, CURLOPT_HEADER, 0);
           curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
           curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
           $result = curl_exec ($ch);
           curl_close($ch);
           return $result;
      
    }
    
    ?>
    Code (markup):
    Note : Uses CURL extension for PHP (recommended) if your server doesn't have CURL with a little change you can make it work ;)
     
    seopup, Jan 1, 2006 IP
    dcristo likes this.
  3. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,199
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #3
    Thanks very much dude! I've been looking for a RSS solution like this for a while now.

    Please PM me your Godaddy details so I can send a domain name your way.

    Another thing, would you happen to know of a php rotation script which enables you to rotate from a selection of different RSS feeds?

    Cheers
     
    dcristo, Jan 1, 2006 IP
  4. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Create a file rsslist.txt with one RSS url per line and after this use this , not sure if will work for all urls but you can try , also edited it here so not 100% that will work :
    
    <?php
    
    $nr_news=6;
    
    $rssurls=file("rsslist.txt");
    $rand=rand(1,count($rssurls))-1;
    $file = $rssurls[$rand];
    
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;
    
    function startElement($parser, $name, $attrs) {
       	global $rss_channel, $currently_writing, $main;
       	switch($name) {
       		case "RSS":
       		case "RDF:RDF":
       		case "ITEMS":
       			$currently_writing = "";
       			break;
       		case "CHANNEL":
       			$main = "CHANNEL";
       			break;
       		case "IMAGE":
       			$main = "IMAGE";
       			$rss_channel["IMAGE"] = array();
       			break;
       		case "ITEM":
       			$main = "ITEMS";
       			break;
       		default:
       			$currently_writing = $name;
       			break;
       	}
    }
    
    function endElement($parser, $name) {
       	global $rss_channel, $currently_writing, $item_counter;
       	$currently_writing = "";
       	if ($name == "ITEM") {
       		$item_counter++;
       	}
    }
    
    function characterData($parser, $data) {
    	global $rss_channel, $currently_writing, $main, $item_counter;
    	if ($currently_writing != "") {
    		switch($main) {
    			case "ITEMS":
    				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
    					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
    				} else {
    					//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
    					$rss_channel[$main][$item_counter][$currently_writing] = $data;
    				}
    				break;
    		}
    	}
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    /*
    if (!($fp = fopen($file, "r"))) {
    	die("could not open XML input");
    }
    
    /*
    while ($data = fread($fp, 4096)) {
    	if (!xml_parse($xml_parser, $data, feof($fp))) {
    		die(sprintf("XML error: %s at line %d",
    					xml_error_string(xml_get_error_code($xml_parser)),
    					xml_get_current_line_number($xml_parser)));
    	}
    }
    */
    $data=curl_string($file);
    xml_parse($xml_parser,$data);
    xml_parser_free($xml_parser);
    
    // putting in array
    $news=array();
    if (isset($rss_channel["ITEMS"])) 
    {
    	if (count($rss_channel["ITEMS"]) > 0) 
    		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) $news[]=$rss_channel["ITEMS"][$i];
    }
    $c=0;
    foreach($news as $key=>$val)
    {
    	if($c<$nr_news)
    	{
    		echo "<p align=left>";
    		echo '<a href="'.$val['LINK'].'">'.$val['TITLE'].'</a> - '.$val['PUBDATE'].'<br>'.''.$val['DESCRIPTION'].'</font></p>';
    	}
    	$c++;
    }
    
    
    function curl_string ($url,$user_agent='Mozilla 4.0'){
    
           $ch = curl_init();
    
           curl_setopt ($ch, CURLOPT_URL, $url);
           curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
           curl_setopt ($ch, CURLOPT_HEADER, 0);
           curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
           curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
           $result = curl_exec ($ch);
           curl_close($ch);
           return $result;
      
    }
    
    ?>
    
    Code (markup):
     
    seopup, Jan 1, 2006 IP
  5. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,199
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #5

    Thanks man, so the txt file goes in the root directory of the server? And the rotation php code just before the rss parser php code on the web page?
     
    dcristo, Jan 1, 2006 IP
  6. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Put php code in a file eg : rss.php and in your pages insert <? include "rss.php" ;?> , both files rss.php and rsslist.php must be in the same directory , or you can put full path to rsslist.txt file eg : /home/user/yoursite/rsslist.txt in rss.php .

    If you use .htm .html files read http://forums.digitalpoint.com/showthread.php?t=47144
     
    seopup, Jan 1, 2006 IP
  7. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is better i think . NOT TESTED on server !

    Put rss.php and rsslist.txt in root of your website .
    In files where you want RSS thing to appear put :
    
    <?
    include ($_SERVER["DOCUMENT_ROOT"]."/rss.php");
    ?>
    
    Code (markup):
    rss.php
    
    <?php
    
    $nr_news=6;
    $rssfile=$_SERVER["DOCUMENT_ROOT"]."/rsslist.txt";
    $rssurls=file($rssfile);
    $rand=rand(1,count($rssurls))-1;
    $file = $rssurls[$rand];
    
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;
    
    function startElement($parser, $name, $attrs) {
       	global $rss_channel, $currently_writing, $main;
       	switch($name) {
       		case "RSS":
       		case "RDF:RDF":
       		case "ITEMS":
       			$currently_writing = "";
       			break;
       		case "CHANNEL":
       			$main = "CHANNEL";
       			break;
       		case "IMAGE":
       			$main = "IMAGE";
       			$rss_channel["IMAGE"] = array();
       			break;
       		case "ITEM":
       			$main = "ITEMS";
       			break;
       		default:
       			$currently_writing = $name;
       			break;
       	}
    }
    
    function endElement($parser, $name) {
       	global $rss_channel, $currently_writing, $item_counter;
       	$currently_writing = "";
       	if ($name == "ITEM") {
       		$item_counter++;
       	}
    }
    
    function characterData($parser, $data) {
    	global $rss_channel, $currently_writing, $main, $item_counter;
    	if ($currently_writing != "") {
    		switch($main) {
    			case "ITEMS":
    				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
    					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
    				} else {
    					//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
    					$rss_channel[$main][$item_counter][$currently_writing] = $data;
    				}
    				break;
    		}
    	}
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    /*
    if (!($fp = fopen($file, "r"))) {
    	die("could not open XML input");
    }
    
    /*
    while ($data = fread($fp, 4096)) {
    	if (!xml_parse($xml_parser, $data, feof($fp))) {
    		die(sprintf("XML error: %s at line %d",
    					xml_error_string(xml_get_error_code($xml_parser)),
    					xml_get_current_line_number($xml_parser)));
    	}
    }
    */
    $data=curl_string($file);
    xml_parse($xml_parser,$data);
    xml_parser_free($xml_parser);
    
    // putting in array
    $news=array();
    if (isset($rss_channel["ITEMS"])) 
    {
    	if (count($rss_channel["ITEMS"]) > 0) 
    		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) $news[]=$rss_channel["ITEMS"][$i];
    }
    $c=0;
    foreach($news as $key=>$val)
    {
    	if($c<$nr_news)
    	{
    		echo "<p align=left>";
    		echo '<a href="'.$val['LINK'].'">'.$val['TITLE'].'</a> - '.$val['PUBDATE'].'<br>'.''.$val['DESCRIPTION'].'</font></p>';
    	}
    	$c++;
    }
    
    
    function curl_string ($url,$user_agent='Mozilla 4.0'){
    
           $ch = curl_init();
    
           curl_setopt ($ch, CURLOPT_URL, $url);
           curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
           curl_setopt ($ch, CURLOPT_HEADER, 0);
           curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
           curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
           $result = curl_exec ($ch);
           curl_close($ch);
           return $result;
      
    }
    
    ?>
    
    Code (markup):
     
    seopup, Jan 1, 2006 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #8
    Magpie doesn't require a backlink afaik and gives you caching - bandwidth saving!
     
    sarahk, Jan 2, 2006 IP
  9. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,199
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #9
    Is that so? To tell you the truth I had only ever tried out Carp previously, I just assumed Magpie would have linked to their own site seeming as though it's so popular. It's all sorted out thanks to seopup so all is good :)
     
    dcristo, Jan 2, 2006 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Cool that it's sorted, but for the benefit of others who may read this thread here's some info on using Magpie RSS
     
    sarahk, Jan 2, 2006 IP
  11. dan101

    dan101 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I have used BNC Advanced rss parser for a while .. it is a very simple script to use especially for non programmers
     
    dan101, Apr 18, 2010 IP
  12. dchoe

    dchoe Active Member

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #12
    dchoe, Apr 19, 2010 IP
  13. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #13
    Probably because it didn't exist in 2006 when the thread was started :)
     
    sarahk, Apr 19, 2010 IP
  14. vidal

    vidal Well-Known Member

    Messages:
    186
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    145
    #14
    Check out RSSparser.com They have several open source and PHP RSS Parsing, XML and Json parsing snippets
     
    vidal, Jun 4, 2012 IP