Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Website Design Forum


You are currently viewing our Website Design Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-16-2007, 09:49 AM PHP RSS Feed
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
I would like to be able to grab some information of another site using php for example iwould like to grab some text of the bbc. and then be to show it on another site.
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
 
Register now for full access!
Old 08-16-2007, 10:31 AM Re: PHP RSS Feed
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Here is an RSS Parser I have in my code library:
Code:
<?php 
class RSSParser { 
 
    var $insideitem = false; 
    var $tag = ""; 
    var $title = ""; 
    var $description = ""; 
    var $link = ""; 
    var $maintitle = ""; 
    var $insidechannel = ""; 
    var $temp;var $id; 
    var $feedid; 
 
    function startElement($parser, $tagName, $attrs) { 
 
            $this->tag = $tagName; 
        if ($tagName == "ITEM") { 
            $this->insideitem = true; 
            $this->insidechannel = false; 
        } 
         else if ($tagName == "CHANNEL") { 
            $this->insidechannel = true; 
            $this->insideitem = false; 
        } 
 
    } 
 
    function RSSParser(){ 
        this->id = abs($feedid."0"); 
        //    $this->feedid = abs($feedid."0"); 
    } 
 
    function endElement($parser, $tagName) { 
        if ($tagName == "ITEM") { 
        if(($this->id - $this->feedid) < 3){ 
            $this->temp .= "<li ><p> <span class=\"toggle\" onclick=\"displayFeed(".$this->id.")\">+/-</span> <a href='".$this->link."' title='".$this->title."'>$this->title</a></p><span class=\"feed\" id=\"".trim($this->title)."\">".$this->description."</span></li>"; 
            $this->title = ""; 
            $this->description = ""; 
            $this->link = ""; 
            $this->insideitem = false; 
            $this->id++; 
        } 
        } else if ($tagName == "CHANNEL") { 
            echo "<h3>".$this->maintitle."</h3><ul >".$this->temp."</ul>"; 
            $this->title = ""; 
            $this->description = ""; 
            $this->link = ""; 
            $this->insideitem = false; 
        } 
    } 
 
    function getID(){ 
        return $this->id; 
    } 
 
    function characterData($parser, $data) { 
        if ($this->insidechannel) { 
                switch ($this->tag) { 
                    case "TITLE": 
                        $this->maintitle .= $data; 
                    break; 
 
                } 
 
        } 
        if ($this->insideitem) { 
        switch ($this->tag) { 
            case "TITLE": 
            $this->title .= $data; 
            break; 
            case "DESCRIPTION": 
            $this->description .= $data; 
            break; 
            case "LINK": 
            $this->link .= $data; 
            break; 
        } 
        } 
    } 
} 
?>
Unfortunately it isn't general so you will need to modify it(Making a general one is on my todo list but I won't get around to it for atleast 3 months)

Then all you need is:

Code:
$xml_parser = xml_parser_create(); 
$rss_parser = new RSSParser(); 
xml_set_object($xml_parser,&$rss_parser); 
xml_set_element_handler($xml_parser, "startElement", "endElement"); 
xml_set_character_data_handler($xml_parser, "characterData"); 
 
$fp = fopen(trim($link),"r") 
    or die("Error reading RSS data."); 
while ($data = fread($fp, 4096)){ 
    xml_parse($xml_parser, $data, feof($fp)); 
} 
 
 
fclose($fp); 
xml_parser_free($xml_parser);
To use it

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-16-2007, 10:41 AM Re: PHP RSS Feed
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Cheers thats defianelty what i need thankyou!

PHP Code:
$xml_parser xml_parser_create(); 
$rss_parser = new RSSParser(); 
xml_set_object($xml_parser,&$rss_parser); 
xml_set_element_handler($xml_parser"startElement""endElement"); 
xml_set_character_data_handler($xml_parser"characterData"); 
 
$fp fopen(trim($link),"r"
    or die(
"Error reading RSS data."); 
while (
$data fread($fp4096)){ 
    
xml_parse($xml_parser$datafeof($fp)); 

 
 
fclose($fp); 
xml_parser_free($xml_parser); 
whats this for?

Last edited by evans123; 08-16-2007 at 10:45 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 08-16-2007, 11:33 AM Re: PHP RSS Feed
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
That sets up the class for parsing purposes, it is what ties it all together. It also reads the rss file, $link is the url for the feed.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-16-2007, 11:38 AM Re: PHP RSS Feed
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
The file i would like to parser isnt an RSS feed is it still possible?
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 08-16-2007, 11:51 AM Re: PHP RSS Feed
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Not with the class I have given you. Different dialects require different markups. Your thread title says RSS feed so this is the one I provided.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Reply     « Reply to PHP RSS Feed
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38211 seconds with 12 queries