home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Programming PHPProgramming PHPSearch this book

11.2. Generating XML

Just as PHP can be used to generate dynamic HTML, it can also be used to generate dynamic XML. You can generate XML for other programs to consume based on forms, database queries, or anything else you can do in PHP. One application for dynamic XML is Rich Site Summary (RSS), a file format for syndicating news sites. You can read an article's information from a database or from HTML files themselves and emit an XML summary file based on that information.

Generating an XML document from a PHP script is simple. Simply change the MIME type of the document, using the header( ) function, to "text/xml". To emit the <?xml ... ?> declaration without it being interpreted as a malformed PHP tag, you'll need to either disable short_open_tag in your php.ini file, or simply echo the line from within PHP code:

<?php
 echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';
?>

Example 11-1 generates an RSS document using PHP. An RSS file is an XML document containing several channel elements, each of which contains some news item elements. Each news item can have a title, a description, and a link to the article itself. More properties of an item are supported by RSS than Example 11-1 creates. Just as there are no special functions for generating HTML from PHP (you just echo it), there are no special functions for generating XML. You just echo it!

Example 11-1. Generating an XML document

<?php header('Content-Type: text/xml'); ?>
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE rss PUBLIC '-//Netscape Communications//DTD RSS 0.91//EN'
 'http://my.netscape.com/publish/formats/rss-0.91.dtd'>
<rss version="0.91">
  <channel>
    <?php
     // news items to produce RSS for
     $items = array(
                    array('title' => 'Man Bites Dog',
                          'link'  => 'http://www.example.com/dog.php',
                          'desc'  => 'Ironic turnaround!'),
                    array('title' => 'Medical Breakthrough!',
                          'link'  => 'http://www.example.com/doc.php',
                          'desc'  => 'Doctors announced a cure for me.')
                    );
  
     foreach($items as $item) {
       echo "<item>\n";
       echo "  <title>{$item[title]}</title>\n";
       echo "  <link>{$item[link]}</link>\n";
       echo "  <description>{$item[desc]}</description>\n";
       echo "  <language>en-us</language>\n";
       echo "</item>\n";
     }
    ?>
  </channel>
</rss>
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE rss PUBLIC '-//Netscape Communications//DTD RSS 0.91//EN'
 'http://my.netscape.com/publish/formats/rss-0.91.dtd'>
<rss version="0.91">
  <channel>
    <item>
  <title>Man Bites Dog</title>
  <link>http://www.example.com/dog.php</link>
  <description>Ironic turnaround!</description>
  <language>en-us</language>
</item>
<item>
  <title>Medical Breakthrough!</title>
  <link>http://www.example.com/doc.php</link>
  <description>Doctors announced a cure for me.</description>
  <language>en-us</language>
</item>
  </channel>
</rss>


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru