PHP Classes
elePHPant
Icontem

File: sample_feeds_oo.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tom Schaefer  >  QXML  >  sample_feeds_oo.php  >  Download  
File: sample_feeds_oo.php
Role: Example script
Content type: text/plain
Description: Sample OO Feed Classes
Class: QXML
XML <> Array (preserve attrib.), JSON, etc.
Author: By
Last change: - typo
Date: 6 years ago
Size: 4,251 bytes
 

Contents

Class file image Download
<?php

Interface IFeed {
    public function
__construct();
    public function
getFeed();
    public function
render();
}

class
Feed implements IFeed{

    protected
$feed;
   
    public function
__construct(){}
   
    public function
getFeed(){
        return
$this->feed;
    }
   
    public function
render(){

        include(
"QXml.class.php");
       
       
header("content-type:text/xml");
       
$xml = new QXML;
       
$xml->noCDATA();
       
$xml->toXML($this->getFeed());
        echo
$xml->asXML();
    }
}

class
AtomFeed extends Feed {

    public function
__construct(){
       
$this->feed["feed"]["@attributes"]["xmlns"] = "http://www.w3.org/2005/Atom";
    }
    public function
setAuthor($name=null){
       
$this->feed["feed"]["author"]["name"] = $name;
    }
    public function
setId($id=null){
       
$this->feed["feed"]["id"]["@textNode"] = $id;
    }
    public function
setEntry($title, $link, $summary, $content, $updated=null){
       
$this->feed["feed"]["entry"][] = array(
               
"title" => array("@textNode" => $title),
               
"link" => array("@attributes"=>$link),
               
"summary" => array("@textNode" => $summary),
               
"updated" => array("@textNode" => ($updated?$updated:date("Y-m-d H:M:S"))),
               
"content" => array("@textNode" => $content),
        );
    }

}

class
RSSFeed extends Feed {

    public function
__construct(){
       
$this->feed["rss"]["@attributes"]["version"] = "2.0";
       
$this->feed["rss"]["channel"]["pubDate"]["@textNode"] = date("Y-m-d H:M:S");
    }
   
    public function
setLink($title = "RSS", $href = "http://www.example.net/feed.rss", $rel = "alternate", $type = "application/rss+xml"){
       
$this->feed["rss"]["link"]["@attributes"] = array("rel" => $rel, "type" => $type, "title" => $title, "href" => $href);
    }
   
    public function
setChannel($title,$link,$description,$lang,$date,$img){
       
$this->feed["rss"]["channel"]["title"]["@textNode"] = $title;
       
$this->feed["rss"]["channel"]["link"]["@textNode"] = $link;
       
$this->feed["rss"]["channel"]["description"]["@textNode"] = $description;
       
$this->feed["rss"]["channel"]["language"]["@textNode"] = $lang;
       
$this->feed["rss"]["channel"]["pubDate"]["@textNode"] = $date;
       
$this->feed["rss"]["channel"]["image"]["@textNode"] = $image;
    }
   
    protected function
setChannelData($name, $data){
       
$this->feed["rss"]["channel"][$name]["@textNode"] = $data;
    }
    public function
setChannelTitle($data){
       
$this->setChannelData("title", $data);
    }
    public function
setChannelDescription($data){
       
$this->setChannelData("description", $data);
    }
    public function
setChannelLanguage($data){
       
$this->setChannelData("language", $data);
    }
    public function
setChannelPubDate($data){
       
$this->setChannelData("pubDate", $data);
    }
    public function
setChannelImage($data){
       
$this->setChannelData("image", $data);
    }

    public function
setItem($title,$link,$description,$author,$guid){
       
$intItem = count($this->feed["rss"]["channel"]["item"]);
       
$this->feed["rss"]["channel"]["item"][$intItem]["title"]["@textNode"] = $title;
       
$this->feed["rss"]["channel"]["item"][$intItem]["link"]["@textNode"] = $link;
       
$this->feed["rss"]["channel"]["item"][$intItem]["description"]["@textNode"] = $description;
       
$this->feed["rss"]["channel"]["item"][$intItem]["author"]["@textNode"] = $author;
       
$this->feed["rss"]["channel"]["item"][$intItem]["guid"]["@textNode"] = $guid;
    }
   
   
}
/*
$feeds = new AtomFeed;
$feeds->setAuthor("Thomas Schäfer");
$feeds->setId("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6");
$feeds->setEntry("Welt Online","http://www.welt.de","Zeitung","adasdasd");
$feeds->setEntry("Spiegel Online","http://www.spiegel.de","Zeitung","adasdasd");
*/

$feeds = new RSSFeed();
$feeds->setLink();
$feeds->setChannelTitle("Test");
$feeds->setChannelDescription("Test Feed");
$feeds->setChannelLanguage("de");
$feeds->setItem("Lorem lipsum","","Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Thomas Schaefer", md5(rand()));
$feeds->setItem("Lorem lipsum2","","Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Thomas Schaefer", md5(rand()));
$feeds->setItem("Lorem lipsum3","","Lorem Ipsum is simply dummy text of the printing and typesetting industry.","Thomas Schaefer", md5(rand()));

$feeds->render();

?>