Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1651 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Display rss feeds using php

Thread title: Display rss feeds using php
Closed Thread    
    Thread tools Search this thread Display Modes  
03-06-2008, 11:58 PM
#1
eXe is offline eXe
Status: I'm new around here
Join date: Jan 2008
Location:
Expertise:
Software:
 
Posts: 19
iTrader: 0 / 0%
 

eXe is on a distinguished road

  Old  Display rss feeds using php

Hi I want to display an rss feed on my hand-coded static html website. I want the content to be crawlable by search engines so I believe a server-side option (like PHP) and not javascript is ideal. Can someone suggest something simple I can use to accomplish this.

Regards & thanks in advance.

03-07-2008, 05:05 AM
#2
CreativeLogic is offline CreativeLogic
CreativeLogic's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,078
iTrader: 6 / 100%
 

CreativeLogic is on a distinguished road

Send a message via MSN to CreativeLogic

  Old

If your site is static, you'll most likely need to convert the static content to a dynamic site using something such as php. Your other option would be to manually update the rss feed (static such as your site) or write a php 'spider' to crawl your site to create the rss feed.

03-07-2008, 09:39 AM
#3
eXe is offline eXe
Status: I'm new around here
Join date: Jan 2008
Location:
Expertise:
Software:
 
Posts: 19
iTrader: 0 / 0%
 

eXe is on a distinguished road

  Old

I understand that adding php code to the page will require the extension of the file to be changed to .php

Also, when I said display I meant that I want another website's feed to appear on my site. My website is a fan-site of a band, and I want the band's rss feed's text to appear on my site.

03-07-2008, 06:08 PM
#4
hadriel is offline hadriel
hadriel's Avatar
Status: I'm new around here
Join date: Mar 2008
Location:
Expertise:
Software:
 
Posts: 22
iTrader: 0 / 0%
 

hadriel is on a distinguished road

  Old

Yep, you can do it, but you fost must allow you to execute php, I guess you know that.

It's also possible to make the server to parse the .html files for php code too. Anyway, you just need a parser in php, which is very easy to find in google.

I'll give you here one class that does the reading, but I have changed it at some points, because it was part of bigger site. Keep in mind that, if it does something you don't know why


Code:
<?php
/*
+--------------------------------------------------------------------------------+
| RSSReader
+--------------------------------------------------------------------------------+
|
|	Description
|	-> Reads a given RSS feed
|
|	Features
|	-> You can customize the initial opening tag by setting
|		the $main_id var
|	-> You can also customize which tags RSSReader will 
|		recognize as sub tags by adding tags to the $sub_keys
|
|	Usage:
|		$rss = new RSSReader("http://http://rss.news.yahoo.com/rss/topstories");
|		$rss->Read();
|		// For complete feed array:
|		print_r($this->feed['array']);
|		// For just the items:
|		print_r($this->feed['items']);
|
|		OR
|
|		$rss = new RSSReader();
|		print_r($rss->Read("http://rss.news.yahoo.com/rss/topstories"));
|
|	Author:			Matt Froese
|	Contact:		matt@spcan.com
|	Version: 		1.0
|	Last Updated:	July 14, 2005	
|
+--------------------------------------------------------------------------------+
*/
class RSSReader {
	
	var $parser 			= '';
    var $file   			= '';
    var $current_tag		= '';
       
    var $main_id			= 'CHANNEL';
    var $sub_keys			= array ('IMAGE', 'ITEM','RSS','RDF:RDF','CHANNEL','ITEMS');	   
    
    var $feed 				= array (	'header' => '',
    									'items' => '' );
    
    var $items              = array();
    var $data 				= array();
    var $feed_data 			= array();
    
    var $errors 			= array();
	var $rdb='';
    
	var $rss_channel = array();
	var $currently_writing = "";
	var $main = "";
	var $item_counter = 0;

	/*	
	+--------------------------------------------------------------------------------+
	| RSSReader
	+--------------------------------------------------------------------------------+	
	*/
    function RSSReader( $file = '' ) {
        $this->file = ( $file == "" ) ? "" : $file;
    }
    
	/*	
	+--------------------------------------------------------------------------------+
	| Read
	+--------------------------------------------------------------------------------+	
	*/
    function Read( $file = '' ) {    	
       	$this->file = ( $file == "" ) ? $this->file : $file;
        if( preg_match("/^http:\/\/([^\/]+)(.*)$/", $this->file, $matches) ) {

            $host 	= $matches[1];
            $uri 	= $matches[2];

            $request = "GET " . $uri . " HTTP/1.0\r\n";
            $request .= "Host: " . $host . "\r\n";
            $request .= "Connection: close\r\n\r\n";

            if( $http = fsockopen($host, 80, $errno, $errstr, 5) ) {
                fwrite($http, $request);
                $timeout = time() + 5;
                
                $response = "";
                while(time() < $timeout && !feof($http)) {
                    $response .= fgets($http, 4096);
                }
				$this->rdb=$response;
                list($header, $xml) = preg_split("/\r?\n\r?\n/", $response, 2);
                if( preg_match("/^HTTP\/[0-9\.]+\s+(\d+)\s+/", $header, $matches) ){
                    $status = $matches[1];
                    if( $status == 200 ) {
						$this->rdb=$xml;
                        $this->parser = xml_parser_create();
                        xml_set_object($this->parser, $this);
                        xml_set_element_handler($this->parser, "startElement", "endElement");
                        xml_set_character_data_handler($this->parser, "characterData");
                        xml_parse($this->parser, trim($xml));
                    } else {
                        $this->errors[] = "Cannot retrieve feed: HTTP returned <b>" . $status . "</b>.";
                    }
                } else {
                    $this->errors[] = "Cannot get status from header.";
                }
            } else {
               $this->errors[] = "Cannot connect to <b>" . $host .  "</b>.";
            }
        } else {
            $this->errors[] = "Invalid file (" . $this->file . ").";
        }
		$this->feed['array'] 	= $this->feed_data;
		$this->feed['items']	= $this->feed_data['ITEM'];
        return $this->rss_channel; //$this->feed['array'];
    }
    
	function justParce($xml){
                        $this->parser = xml_parser_create();
                        xml_set_object($this->parser, $this);
                        xml_set_element_handler($this->parser, "startElement", "endElement");
                        xml_set_character_data_handler($this->parser, "characterData");
                        xml_parse($this->parser, trim($xml));
	
		$this->feed['array'] 	= $this->feed_data;
		$this->feed['items']	= $this->feed_data['ITEM'];
        return $this->rss_channel; //$this->feed['array'];
	}
	function getResponse(){
		return $this->rdb;
	}
	/*	
	+--------------------------------------------------------------------------------+
	| startElement
	+--------------------------------------------------------------------------------+	
	*/
    function startElement($parser, $name, $attrs) {
        /*$this->current_tag = $name;
    	
        if( $this->current_tag == $this->main_id ) {
            $this->inside_tag[ $this->main_id ] = true;
    	}
    	foreach( $this->sub_keys as $key ) {
    		if( $this->current_tag == $key ) {
    			$this->inside_tag[ $key ] = true;
    			break;
    		}
    	}
		*/
		
	  	switch($name) {
   		case "RSS":
   		case "RDF:RDF":
   		case "ITEMS":
   			$this->currently_writing = "";
   			break;
   		case "CHANNEL":
   			$this->main = "CHANNEL";
   			break;
   		case "IMAGE":
   			$this->main = "IMAGE";
   			$this->rss_channel["IMAGE"] = array();
   			break;
   		case "ITEM":
   			$this->main = "ITEMS";
   			break;
   		default:
   			$this->currently_writing = $name;
   			break;
		}
    }

	/*	
	+--------------------------------------------------------------------------------+
	| characterData
	+--------------------------------------------------------------------------------+	
	*/
    function characterData($parser, $data) { 
    	// skip if this element == ""
    /*	if( trim($data) != "" ) {
	    	if( $this->inside_tag[ $this->main_id ] ) { 			
				$done = false;
		    	foreach( $this->sub_keys as $key ) {
		    		if( isset($this->inside_tag[ $key ]) && $this->inside_tag[ $key ] == true ) {
		    			if( isset($this->data[ $key ][ $this->current_tag ]) ) {
		    				$this->data[ $key ][ $this->current_tag ] 	.= $data;
		    			} else {
		    				$this->data[ $key ][ $this->current_tag ] 	= $data;	
		    			}
		    			$done 										= true;
		    			break;
		    		}
		    	}
	    		if( $done == false ) {
	    			$this->feed_data[ $this->current_tag ] = $data;		
	    		}			
	    	} 
    	}
*/


	if ($this->currently_writing != "") {
		switch($this->main) {
			case "CHANNEL":
				if (isset($this->rss_channel[$this->currently_writing])) {
					$this->rss_channel[$this->currently_writing] .= $data;
				} else {
					$this->rss_channel[$this->currently_writing] = $data;
				}
				break;
			case "IMAGE":
				if (isset($this->rss_channel[$this->main][$this->currently_writing])) {
					$this->rss_channel[$this->main][$this->currently_writing] .= $data;
				} else {
					$this->rss_channel[$this->main][$this->currently_writing] = $data;
				}
				break;
			case "ITEMS":
				if (isset($this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
					$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
				} else {
					//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
					$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
				}
				break;
		}
	}

	
    }

	/*	
	+--------------------------------------------------------------------------------+
	| endElement
	+--------------------------------------------------------------------------------+	
	*/
    function endElement($parser, $name) {    
    	    	
    /*	foreach( $this->sub_keys as $key ) {
    		if( $name == $key ) {
    			$this->inside_tag[ $key ] 					= false;    			
    			$this->feed_data[ $key ][] 	= $this->data[ $key ];
    			$this->data[ $key ] 						= array();
    			break;
    		}
    	}
    	if( $name == $this->main_id ) {
            $this->inside_tag[ $this->main_id ] = false;
    	}    */
        
        $this->current_tag = "";
		
	   	$this->currently_writing = "";
		if ($name == "ITEM") {
			$this->item_counter++;
		}
    }
}


?>

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed