教你实现RSS订阅功能

本文介绍了一个使用 PHP 生成 RSS Feed 的实例。通过设置正确的头部信息并构造 XML 结构,可以创建符合 RSS 2.0 规范的 Feed 文件。示例包括频道的基本元数据及多个条目。

<?php
header("Content-type:application/xml");//表示本段代码是一个xml文件,非常重要

       


$xmlstr = '<rss version="2.0"  encoding="utf-8">
    <channel>
        <title>Personal News Site</title>
        <description>daily news from me</description>
        <link>http://mydomain.net/</link>
        <lastBuildDate>Sat, 19 Jun 2010 08:55:01 GMT</lastBuildDate>
        <generator>FeedCreator 1.8.0-dev (info@mypapit.net)</generator>
        <copyright>feedcreator (c) 2006</copyright>
        <category>Entertainment</category>
        <item>
            <title>测试成功</title>
            <link>http://mydomain.net/news/somelinks.html</link>
            <description><![CDATA[<p><strong>hahaha aku berjaya!</strong></p>]]></description>
            <author> JohnDoe@example.com (John Doe)</author>
            <category>normal</category>
            <guid isPermaLink="false">http://mydomain.net/news/somelinks.html</guid>
            <guid isPermaLink="false">urn:feeds-archive-org:validator:1</guid>
            <enclosure url="http://mydomain.net/news/picture.jpg" length="65905" type="image/jpeg"/>
        </item>
        <item>
            <title>test berita pertama</title>
            <link>http://mydomain.net/news/somelinks.html</link>
            <description><![CDATA[<p><strong>hahaha aku berjaya!</strong></p>]]></description>
            <author> JohnDoe@example.com (John Doe)</author>
            <category>normal</category>
            <guid isPermaLink="false">http://mydomain.net/news/somelinks.html</guid>
            <guid isPermaLink="false">urn:feeds-archive-org:validator:1</guid>
            <enclosure url="http://mydomain.net/news/picture.jpg" length="65905" type="image/jpeg"/>
        </item>
        <item>
            <title>test berita pertama</title>
            <link>http://mydomain.net/news/somelinks.html</link>
            <description><![CDATA[<p><strong>hahaha aku berjaya!</strong></p>]]></description>
            <author> JohnDoe@example.com (John Doe)</author>
            <category>normal</category>
            <guid isPermaLink="false">http://mydomain.net/news/somelinks.html</guid>
            <guid isPermaLink="false">urn:feeds-archive-org:validator:1</guid>
            <enclosure url="http://mydomain.net/news/picture.jpg" length="65905" type="image/jpeg"/>
        </item>
        <item>
            <title>test berita pertama</title>
            <link>http://mydomain.net/news/somelinks.html</link>
            <description><![CDATA[<p><strong>hahaha aku berjaya!</strong></p>]]></description>
            <author> JohnDoe@example.com (John Doe)</author>
            <category>normal</category>
            <guid isPermaLink="false">http://mydomain.net/news/somelinks.html</guid>
            <guid isPermaLink="false">urn:feeds-archive-org:validator:1</guid>
            <enclosure url="http://mydomain.net/news/picture.jpg" length="65905" type="image/jpeg"/>
        </item>
        <item>
            <title>test berita pertama</title>
            <link>http://mydomain.net/news/somelinks.html</link>
            <description><![CDATA[<p><strong>hahaha aku berjaya!</strong></p>]]></description>
            <author> JohnDoe@example.com (John Doe)</author>
            <category>test</category>
            <guid isPermaLink="false">http://mydomain.net/news/somelinks.html</guid>
            <guid isPermaLink="false">urn:feeds-archive-org:validator:1</guid>
            <enclosure url="http://mydomain.net/news/picture.jpg" length="65905" type="image/jpeg"/>
        </item>
    </channel>
</rss>';
echo $xmlstr;

注释:文件一定要是UTF8类型;否则中文会出错;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值