将PHP输出转换为XML - 创建RSS提要

时间:2012-07-15 08:35:50

标签: php xml

当前代码将输出提供给“li”,我想将输出调整为有效的XML,以便我可以从该列表创建RSS提要。有什么建议吗?

到目前为止,这是我的代码:

<?php
session_start();
mb_internal_encoding( 'UTF-8' );
require_once('../config.php');
require_once('../dbconnect.php');
echo ' <div class="tagsz clearfix"> ';
echo "<ul id='mytags_ara'>";
echo "<h2 class='title'>English Trends</h2>";
echo ' <div class="tagsz clearfix"> ';
 echo "<ul id='mytags_en'>";
 $query = "SELECT hashtag, sum(count) as total FROM `trending_topics` WHERE lang=0   and  hashtag != '' and date >= date_sub(left(now(), 10), interval 1 day) group by hashtag      order by total desc";
  $res = mysql_query($query);
 $index = 0;
 $hashtags = null;
 while($row = mysql_fetch_assoc($res) ) {
 $hashtag = $row['hashtag'];
 if( strtolower($hashtag) != 'newnew' && strtolower($hashtag) != 'new' && strtolower  ($hashtag) != 'more' )  {
 echo "<li><a class='size".$index."'".$hashtag."'>#".$hashtag."</a></li>";
 $index++;
 }
 if($index==6) break;}
 echo "</ul>";
 echo "</div>";
 ?>

2 个答案:

答案 0 :(得分:1)

这可能会或可能不会帮助您,但这是我用来生成Atom Feed的代码:

<?php
    define('DS', DIRECTORY_SEPARATOR);
    define('ROOT', dirname(dirname(__file__)));
    define('HOST', 'http://' . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] ) );

    function publishAtom() {
            /*
            My data entities had these fields:
            - title
            - category
            - permalink (a URL-safe version of the title)
            - timestamp
            - lastupdated
            - abstract

            You'll need to adjust the code below to match your entities.
            */

            # get the data
            $rows = //<<insert your data-gathering-code here>> -> needs to be an array of your entities;


            $feed_title = "Atom Feed";
            $feed_subtitle = "Get all our newest entries when you subscribe to the feed.";
            $site_domain = "http://yourdomain.com";
            $author_name = "Your Name";
            $author_email = "yourname@yourdomain.com";

            $feed = '<?xml version="1.0" encoding="utf-8"?>
            <feed xmlns="http://www.w3.org/2005/Atom">
                <title>' . $feed_title . '</title>
                <subtitle>' . $feed_subtitle . '</subtitle>
                <link href="' . $site_domain . '/atom.xml" rel="self" />
                <link href="' . $site_domain . '" />
                <updated>' . date('c') . '</updated>
                <author>
                    <name>' . $author_name . '</name>
                    <email>' . $author_email . '</email>
                </author>
                <id>tag:' . str_replace( "http://", '', $site_domain ) . ',2012:feed</id>';

            # Get the entries


            foreach( $rows as $row ) {  
                $feed .= '<entry>
                    <title>' . $row->title . '</title>
                    <link href="' . strtolower( $site_domain . DS . $row->category . DS . $row->permalink ) . '" />';

                    $date= date( 'Y-m-d', strtotime( $row->timestamp ) );
                    $uuid = str_replace( "http://", '', $site_domain ) . ',' . $date . ':' . $row->permalink;

                $feed .= '<id>tag:' . $uuid . '</id>';

                if( isset( $row->lastupdated ) ) {
                    $feed .= '<updated>' . date( 'c', strtotime( $row->lastupdated ) ) . '</updated>';
                }
                else {
                    $feed .= '<updated>' . date( 'c', strtotime( $row->timestamp ) ) . '</updated>';
                }

                $feed .= '<summary>Entry for ' . $date . '</summary>
                    <content type="xhtml" xml:lang="en">
                        <div xmlns="http://www.w3.org/1999/xhtml">' . $row->abstract . '</div>
                    </content>
                </entry>';
            }

            $feed .= "</feed>";

            $path = ROOT . DS . "atom.xml";
            $filenum=fopen( $path, "w" );
            fwrite( $filenum, $feed );
            fclose( $filenum );
        }
    }

    # call this function wherever is relevent for you
    publishAtom();
?>

它应该让您了解如何手动执行此操作。 HTH。

答案 1 :(得分:0)

你试过SimpleXML吗? SimpleXML是一个很棒的包,可以很容易地解析和创建XML样式文档。

针对您的具体情况,请尝试以下操作:http://snipplr.com/view/61231/

修改 以下是SimpleXML的完整文档:http://in.php.net/simplexml