To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 07-10-2008, 07:46 AM   #1
iceomnia
Senior Member
 
Join Date: Oct 2007
Posts: 164
A quick PHP XMLWriter Class Tutorial (XML & RSS)

Since there is absolutely no documentation on how to use PHP5's XMLWriter class, here is a very simple example of how to use the class to create an rss feed.

PHP Code:
<?php

// THIS IS ABSOLUTELY ESSENTIAL - DO NOT FORGET TO SET THIS
@date_default_timezone_set("GMT");

$writer = new XMLWriter();
// Output directly to the user

$writer->openURI('php://output');
$writer->startDocument('1.0');

$writer->setIndent(4);

// declare it as an rss document
$writer->startElement('rss');
$writer->writeAttribute('version', '2.0');
$writer->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');


$writer->startElement("channel");
//----------------------------------------------------
//$writer->writeElement('ttl', '0');
$writer->writeElement('title', 'Latest Products');
$writer->writeElement('description', 'This is the latest products from our website.');
$writer->writeElement('link', 'http://www.domain.com/link.htm');
$writer->writeElement('pubDate', date("D, d M Y H:i:s e"));
    
$writer->startElement('image');
        
$writer->writeElement('title', 'Latest Products');
        
$writer->writeElement('link', 'http://www.domain.com/link.htm');
        
$writer->writeElement('url', 'http://www.iab.net/media/image/120x60.gif');
        
$writer->writeElement('width', '120');
        
$writer->writeElement('height', '60');
    
$writer->endElement();
//----------------------------------------------------



//----------------------------------------------------
$writer->startElement("item");
$writer->writeElement('title', 'New Product 8');
$writer->writeElement('link', 'http://www.domain.com/link.htm');
$writer->writeElement('description', 'Description 8 Yeah!');
$writer->writeElement('guid', 'http://www.domain.com/link.htm?tiem=1234');

$writer->writeElement('pubDate', date("D, d M Y H:i:s e"));

$writer->startElement('category');
    
$writer->writeAttribute('domain', 'http://www.domain.com/link.htm');
    
$writer->text('May 2008');
$writer->endElement(); // Category

// End Item
$writer->endElement();
//----------------------------------------------------


// End channel
$writer->endElement();

// End rss
$writer->endElement();

$writer->endDocument();

$writer->flush();
?>
The above will give you this:

Code:
<?xml version="1.0"?> 
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> 
<channel> 
 <title>Latest Products</title> 
 <description>This is the latest products from our website.</description> 
 <link>http://www.domain.com/link.htm</link> 
 <pubDate>Thu, 10 Jul 2008 11:47:14 GMT</pubDate> 
 <image> 
  <title>Latest Products</title> 
  <link>http://www.domain.com/link.htm</link> 
  <url>http://www.iab.net/media/image/120x60.gif</url> 
  <width>120</width> 
  <height>60</height> 
 </image> 
 <item> 
  <title>New Product 8</title> 
  <link>http://www.domain.com/link.htm</link> 
  <description>Description 8 Yeah!</description> 
  <guid>http://www.domain.com/link.htm?tiem=1234</guid> 
  <pubDate>Thu, 10 Jul 2008 11:47:14 GMT</pubDate> 
  <category domain="http://www.domain.com/link.htm">May 2008</category> 
 </item> 
</channel> 
</rss>
Just a note from an rss validator, this line should be before the 'channel' tag for some reason!

Code:
<atom:link href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" />
I will post another example below for the php5 XMLReader Class later.
__________________
*** Please read this when pasting your code into this website ***

Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.

Last edited by iceomnia; 07-10-2008 at 08:01 AM.
iceomnia is offline   Reply With Quote
Old 07-10-2008, 08:33 AM   #2
iceomnia
Senior Member
 
Join Date: Oct 2007
Posts: 164
...and here's how to use it as a pre made class...

PHP Code:
@date_default_timezone_set("GMT");


class
rss extends XMLWriter
{
    
// $file = filename to output to
    // $title = rss fed title
    // $description = rss feed description
    // $link = rss feed link
    // $date = date of feed
    
    
function __construct($file, $title, $description, $link, $date)
    {
        
// Start by calling the XMLWriter constructor...
        
        
$this->openURI($file);
        
$this->startDocument('1.0');
        
$this->setIndent(4);
        
        
$this->startElement('rss');
            
$this->writeAttribute('version', '2.0');
            
$this->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');

        
$this->startElement("channel");
            
$this->writeElement('title', $title);
            
$this->writeElement('description', $description);
            
$this->writeElement('link', $link);
            
$this->writeElement('pubDate', date("D, d M Y H:i:s e", strtotime($date)));
        
    }
    
    
// Send a multi-dimensional array
    // $array =
    // 'title' = Item title
    // 'descritpion'
    // 'link'
    // 'guid' = unique id for item (should be a url)
    
    // 'category' = array of:
    // 'title'
    // 'domain'
    
    
function addItem($array)
    {
        if (
is_array($array))
        {
            
$this->startElement("item");
            
$this->writeElement('title', $array['title']);
            
$this->writeElement('link', $array['link']);
            
$this->writeElement('description', $array['description']);
            
$this->writeElement('guid', $array['guid']);
            
            if (isset(
$array['date']))
            {
                
$this->writeElement('pubDate', date("D, d M Y H:i:s e", strtotime($array['date'])));
            }
            
            if (isset(
$array['category']) && isset($array['category']['title']))
            {
                
$this->startElement('category');
                    
$this->writeAttribute('domain', $array['category']['domain']);
                    
$this->text($array['category']['title']);
                
$this->endElement(); // Category
            
}
        
$this->endElement(); // Item
        
}
        
    }
    
    function
_endRss()
    {
        
// End channel
        
$this->endElement();
        
        
// End rss
        
$this->endElement();
        
        
$this->endDocument();
        
        
$this->flush();
    }
    
}
// end class...
...and a sample on the class usage!

PHP Code:
// Sample usage of class...

$item = array();
$item['title'] = 'New product One';
$item['link'] = 'http://www.domain.com/product1.htm';
$item['description'] = 'A full description of product that is new.';
$item['guid'] = 'http://www.domain.com/product1.htm'; // a unique http address will do!
$item['date'] = date('Y-m-d'); //send any time of date!

$item['category'] = array();
$item['category']['title'] = 'CD Players';
$item['category']['domain'] = 'http://www.domain.com/cdplayers.htm';

$w = new rss('php://output', 'New Products', 'This month\'s new products', 'http://www.domain.com/link.htm', date('Y-m-d'));
    
$w->addItem($item);

$w->_endRss();
You could use a destructor, but I think the '_endRss' is better.

Next project... PHP XMLReader
__________________
*** Please read this when pasting your code into this website ***

Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.

Last edited by iceomnia; 07-10-2008 at 10:03 AM.
iceomnia is offline   Reply With Quote
Old 02-17-2009, 06:14 PM   #3
iceomnia
Senior Member
 
Join Date: Oct 2007
Posts: 164
I'm just writing an extension to XMLReader - this will give you a good example of how to manipulate an RSS XML news feed for your website...

I'll post a link here when it's ready...

__________________

Web Design <- PHP Website Design & Programming
__________________
*** Please read this when pasting your code into this website ***

Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.
iceomnia is offline   Reply With Quote
Old 12-04-2009, 07:17 PM   #4
tekneck
Junior Member
 
Join Date: Dec 2009
Posts: 1
Red face How To Add A Looping Section?

In the example below (btw, thanks so much for this class and the details!), how would I make a looping section.

I want to include a bunch of images in each $item and keep overwriting the variables...

ie:

<item>
<title>Title</title>
<link>link url</link>
<images>
<image>
<title>image title</title>
<url>imageurl</ul>
</image>
<image>
<title>image title</title>
<url>imageurl</ul>
</image>
<image>
<title>image title</title>
<url>imageurl</ul>
</image>
</images>
</item>

When I use the code below and the class, I only get one image (the last one from my while sql query results)

When I make the array key->values unique by adding a counter (eg: key1->value1,key2->value2 etc) I get all my images under one <image>array data</image> instead of multiple <image>stuff from first record</image><image>stuff from second record</image> etc...

Here is what I have done... perhaps you can help me with this concept.. I am not sure if XMLWriter supports what I am trying to do...






PHP Code:

if (isset($array['images']))
            {
                
$this->startElement('media');
                    
                        
$this->startElement('image');
                        
                        foreach (
$array['images'] as $key => $value)
                        {
                            
$this->writeElement($key, $value);
                        }
                        
$this->endElement();
                
                
                
$this->endElement();
            }
And to set the array in my code I am doing:

PHP Code:

if(mysql_num_rows($images))
        {
            
$item['images'] = array();
            
$image_count = 1;
            
            while(
$image_row = mysql_fetch_assoc($images))
            
            {
                
$item['images']['image_'.$image_count.'_seq'] = $image_row['display_order'];
                
$item['images']['image_'.$image_count.'_filename'] = $image_row['full_filename'];
                
$image_count ++;
            }
        }
So I am close to what I needed but not all the way...

Ideas, gurus?
tekneck is offline   Reply With Quote
Reply

Bookmarks


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

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:59 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.