search
HomeBackend DevelopmentPHP TutorialCreate your own RSS client with PHP package_PHP Tutorial
Create your own RSS client with PHP package_PHP TutorialJul 13, 2016 am 10:38 AM
phprsscreateBagclientuseprogramSimplepolymerizationmy own

RSS,也叫做真正简单聚合(Really Simple Syndication)或者RDF站点摘要(RDF Site Summary),是一个让Web网站向用户发布和聚合最新内容的文件格式。RSS的“feed”用XML来表示;这样做的结果是,它能够被任何具备分析XML文件的客户端读取。现在这样的RSS客户端软件很多,用于Windows和Linux平台的都有,最新版本的Mozilla Firefox和Internet Explorer都允许你订阅所需要的RSS feed,以保证你的手头总有最新的信息。

就像很多优秀的编程语言一样,PHP通过PEAR XML_RSS程序包对读取和创建RSS feed提供了支持。这个程序包是一个预先编译的代码库,它可以让你从RSS feed里提取信息,并把它们转换成另外一个格式(例如,MySQL数据库或者文本文件),或者如果你想要自定义创建一个能够从多个RSS源收集信息的Web页面。

在本文里,我将讲解后面一种情况,告诉你如何使用PEAR XML_RSS包,把来自多个RSS feed的新闻标题集成到一个Web页面上。我现在假设你已经安装好了一个工作正常的Apache和PHP,而且你已经成功地下载和安装了PEAR XML_RSS程序包和依赖关系。


开始吧

现在就让我们从一个简单的例子开始,它将告诉你XML_RSS是如何工作的。首先创建下面的脚本(列表A):

列表A

// include class
include ("RSS.php");

// download and parse RSS data
$rss =& new XML_RSS("http://techrepublic.com.com/5150-22-0.xml");
$rss->parse();

// print headlines
print_r($rss->getItems());
?>

在这里,脚本会读取类定义,然后实例化一个新的XML_RSS()对象。对象的构造函数用来传递元数据的URL——这在本文里就是TechRepublic的RSS feed。然后,调用parse()方法来分析XML和从中提取信息。最后,getItems()方法会返回一个结构清晰的嵌套数组,也就是从feed中提取出来的新闻项目。每一个项目都有一个标题、一段描述、一个发表日期,以及链接到完整文章的URL ,就像下面显示的输出一样(列表B):

列表B

Array
(
    [0] => Array
        (
            [title] => Bump the size of your information store to 75GB (Exchange 2003 Standard Edition only)
            [link] => http://techrepublic.com.com/5100-1035_11-6063252.html?
part=rss&tag=feed&subj=tr
            [description] => In Service Pack 2, the Exchange developers
have provided you with the ability to size the information store to any size you like between 1 and 75 GB, and they chose 18GB as the
default. Here's how to change the size yourself.
            [pubdate] => Fri, 21 Apr 2006 00:00:00 PDT
        )

    [1] => Array
        (
            [title] => Learn the pros and cons of Windows Firewall
            [link] => http://techrepublic.com.com/5100-1009_11-6063367.html?
part=rss&tag=feed&subj=tr
            [description] => Is Windows Firewall up to the task of securing your network? Mike Mullins has
his doubts. In this edition of Security Solutions, he delves into the details of Windows Firewall and weighs its pros and cons.
            [pubdate] => Thu, 20 Apr 2006 13:25:00 PDT
        )

...
)

提取关于feed本身的源信息也是可能的,把调用getItems()改成调用getChannelInfo()就可以了。正如其名字所表示的,这个方法用来返回与feed本身相关的信息,包括题目和描述(如果有的话)。下面就是它的代码(列表C):

列表C

// include class
include ("RSS.php");

// download and parse RSS data
$rss =& new XML_RSS("http://techrepublic.com.com/5150-22-0.xml");
$rss->parse();

// print channel information
print_r($rss->getChannelInfo());
?>

下面是输出结果(列表D):

列表D

Array
(
    [title] => TechRepublic.com
    [link] => http://www.techrepublic.com/
    [description] => Real World. Real Time.Real IT.
)

Use a single feed

As the previous example shows, XML_RSS does a pretty good job of parsing an RSS feed and converting it into a PHP array. Once this array is generated, it is fairly easy to process it into a format suitable for display on a Web site. The following example illustrates this (Listing E):

List E



The latest from TechRepublic:


    // include class
    include ("RSS.php");

    // download and parse RSS data
    $rss =& new XML_RSS("http://techrepublic.com.com/5150-22-0.xml");
    $rss->parse ();

    // print channel information
    foreach ($rss->getItems() as $item) {
    echo "

  • " . $item['title'] . "
    ";
    echo $item['description'] . " (" . $item['pubdate '] . ")

    ";
    }
    ?>



In this article, the array returned by getItems() is processed using a foreach() loop. Each element of the array is itself an array, and the elements include the news title, description, and publication date. These elements are extracted and formatted into an unsorted HTML list of elements. Figure A shows you an example of this:

Create your own RSS client with PHP package_PHP Tutorial

Array elements

Use multiple feeds

How can one feed be enough? With a little creative code you can add feeds at will! List F is such a piece of code:

List F



// include class
include ("RSS.php");

// set up array of RSS feeds
$feeds = array( "http://techrepublic.com.com/5150-22-0.xml",
                 "http://news.linux .com/news.rss",
"http://rss.slashdot.org/Slashdot/slashdot");

// retrieve each feed
// get channel information and headlines
foreach ($feeds as $f) {
$rss =& new XML_RSS($f);
$rss- >parse();
$info = $rss->getChannelInfo();
$items = $rss->getItems();

// print channel information
?>
The latest from :


    // print headlines and descriptions
    foreach ($items as $item) {
    echo "

  • " . $item['title'] . "
    ";
                                                                                                                                              . ";
    }
    ?>


}
?>


The modification to the previous example code is simple and clear. I created an array of URLs to different feeds and used a loop to process the array without hard-wired linking the URLs to the feeds in the object constructor. Each iteration of the loop creates a new XML_RSS object with a different source feed; this feed is then processed in the normal way, that is, by calling the parse() and getItems() methods. Other improvements are the use of the getChannelInfo() method, discussed earlier, which is used to dynamically display the feed's name and URL at the top of each title list.

The following is an example of the output result (Figure B):

Create your own RSS client with PHP package_PHP Tutorial

More than one RSS feed


Of course, you can modify this structure to more closely reflect your needs. For example, the script will instantly display all news headlines in each feed; for example, you can change it to only display the first 5 headlines in each feed, just use a for() loop and in the second nesting level Just use a counter. You can also reformat the page layout to display news headlines in a drop-down menu, allowing for a different type of browsing. Give it a try and have fun!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735108.htmlTechArticleRSS, also called Really Simple Syndication or RDF Site Summary, is a A file format that allows Web sites to publish and aggregate the latest content to users...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.