当前位置: 首页 > 工具软件 > SimplePie > 使用案例 >

使用SimplePie消费Feed

卫浩瀚
2023-12-01

If you’re an avid feed consumer like I am, you might be a bit sad to see Google’s recent announcement of Reader reaching end of life. Reader was simple and easy to use, but there’s no reason you can’t have the same functionality with your own homegrown project. The PHP library SimplePie allows for quick and easy feed consumption and display. Here’s how you can get started on your own feed reader.

如果你是一个狂热的饲料消费像我,你可能会有点难过,看到谷歌最近的读者达到了生命的尽头公告。 Reader既简单又易于使用,但是没有理由不能与自己的本地项目拥有相同的功能。 PHP库SimplePie允许快速方便地使用和显示提要。 您可以按照以下方法开始使用自己的供稿阅读器。

SimplePie is installable via Composer. You’ll need to add the following to your composer.json file. After Composer downloads the library and you include the autoloader file in your PHP script, you’re ready to begin writing your very own reader.

通过Compose r 安装 SimplePie。 您需要将以下内容添加到您的composer.json文件中。 在Composer下载库并在PHP脚本中包含自动加载器文件之后,就可以开始编写自己的阅读器了。

{
    "require": {
        "simplepie/simplepie": "dev-master"
   }
}

基本功能 (Basic Functionality)

To work with SimplePie, you’ll need first to pick a RSS or Atom feed you’d like to manipulate and grab its URL. I’ll be using the New York Times for my examples – here’s what you should have to start:

要使用SimplePie,您首先需要选择您要操作的RSS或Atom提要并获取其URL。 我将以《纽约时报》为例-这是您必须开始的内容:

<?php
require_once 'autoloader.php';

$url = 'http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml';
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();

You can see in the above code the URL to the NY Times’ feed, and the beginning of our SimplePie-based reader. In the latest version of SimplePie (v1.3), the constructor doesn’t take any arguments, unlike previous versions, and we use the set_feed_url() method to tell it where to pull the feed data from. Once the URL is set, we call init() and the reader is ready to go. You won’t see anything with just the above though; we still need to go out and grab the information.

您可以在上面的代码中看到《纽约时报》提要的URL,以及我们基于SimplePie的阅读器的开头。 在最新版本的SimplePie(v1.3)中,与以前的版本不同,构造函数不接受任何参数,并且我们使用set_feed_url()方法告诉它从何处提取提要数据。 设置完URL后,我们将调用init() ,阅读器已准备就绪。 但是,仅凭上述内容您将看不到任何东西; 我们仍然需要出去获取信息。

SimplePie provides many classes and methods to extract the information from any given section of an RSS feed. With the above beginning, Let’s add the following:

SimplePie提供了许多类和方法来从RSS feed的任何给定部分提取信息。 从以上开始,让我们添加以下内容:

<?php
echo '<h1>' . $feed->get_title() . '</h1>';
echo '<p>' . $feed->get_description() . '</p>';

The methods get_title() and get_description() do exactly what they say; they return the title and the description of the entire feed. You can get the feed author (in some instances, but not with the NY Times feed), feed contributors, feed link(s), copyright, and of course items with similar methods.

方法get_title()get_description()完全按照它们说的去做。 他们返回整个提要的标题和描述。 您可以使用类似的方法来获取供稿作者(在某些情况下,但不适用于《纽约时报》供稿),供稿提供者,供稿链接,版权以及当然还有项目。

In order to actually display any stories posted by the feed, you need to get at minimum one item using the get_item() or get_items() functions. For now, let’s just grab a single item and get some information from it.

为了实际显示提要所发布的任何故事,您至少需要使用get_item()get_items()函数获得一项。 现在,让我们仅获取一个项目并从中获取一些信息。

<?php
$item = $feed->get_item(0);
echo '<article>';
echo '<header>';
echo '<p>Title: <a href="' . $item->get_link() . '">' . $item->get_title() . '</a></p>';
echo '<p>Author: ' . $item->get_author()->get_name() . '</p>';
echo '<p>Date: ' . $item->get_date('Y-m-d H:i:s') . '</p>';
echo '<p>Description: ' . $item->get_description() . '</p>';
echo '</header>';
echo $item->get_content(true);
echo '</article>';

This certainly won’t look pretty, but it shows you a the basics necessary for getting information from a feed item. So, let’s go over what some of this means.

这当然看起来并不漂亮,但是它向您展示了从提要项目中获取信息的必要基础知识。 因此,让我们回顾一下其中的一些含义。

The get_item() method retrieves a single item from the feed. The integer argument provided is the feed item number and, like an array, zero is the index of the first item in the feed.

get_item()方法从提要中检索单个项目。 提供的整数参数是提要项目编号,并且像数组一样,零是提要中第一项的索引。

The get_link() method returns a URL to the feed item itself, allowing you to open the article/video/etc for whatever feed you are working with. The get_title and get_description() methods should look familiar as they are the same as before when being applied directly to the feed itself.

get_link()方法将URL返回到提要项本身,从而使您可以为正在使用的任何提要打开文章/视频/等。 get_titleget_description()方法应该看起来很熟悉,因为它们直接应用于提要本身时与以前一样。

The author line looks a little funny, but as I’m sure you’ve figured out – the get_author() method returns an Author object, and we call the get_name() method on that object to retrieve whom the article is by.

作者行看起来有些有趣,但是我敢肯定,您已经弄清楚– get_author()方法返回Author对象,我们在该对象上调用get_name()方法以检索文章的作者。

The get_date() method takes any standard PHP date format string to display the date however you like.

get_date()方法采用任何标准PHP日期格式字符串来显示您想要的日期。

Finally, the get_content() method takes a Boolean argument that says either return only the summary information (true) or try to return the summary information but fallback to the description if none is provided (false).

最后, get_content()方法采用布尔参数,该布尔参数表示仅返回摘要信息(true)或尝试返回摘要信息,但如果未提供任何内容,则返回描述(false)。

选择项目 (Selecting Items)

So we’ve seen the basics and have displayed a feed title, description, and the relevant information for a single item posted to the feed. That’s all well and good, but it would be frustrating to have to refresh the page constantly to see if there is a newer item, and even more frustrating that we can’t see any past items!

因此,我们已经了解了基础知识,并显示了供稿标题,描述以及发布到供稿的单个项目的相关信息。 很好,但是必须不断刷新页面以查看是否有新项目会令人沮丧,更令人沮丧的是我们看不到任何过去的项目!

SimplePie provides for that very easily, and I’ve already briefly touched on our options: get_item() and get_items(). These two functions are used to get our feed items and their content in two different ways.

SimplePie可以很容易地实现这一目的,而我已经简要介绍了我们的选项: get_item()get_items() 。 这两个函数用于以两种不同的方式获取我们的提要项及其内容。

The first option that we demoed takes a single integer that specifies which item in the “array” of feed items we want to grab. Using this in a loop in conjunction with the method get_item_quantity() allows us to display all of or a subset of the items in the feed.

我们演示的第一个选项采用单个整数,该整数指定我们要获取的Feed项“数组”中的哪个项。 将此循环与方法get_item_quantity()结合使用可以使我们显示提要中所有或部分项目。

<?php
$itemQty = $feed->get_item_quantity();
for ($i = 0; $i < $itemQty; $i++) {
...
}

This solution, however usable, is not very elegant if you want to use pagination when displaying feed items. For this, we can use the method get_items(). It accepts two integers: an offset, and an item count. This allows us to get all of the items by passing zeros for both, or, what is really handy about this, allows us to get small subsets anywhere throughout all available feed items.

如果要在显示提要项目时使用分页,则此解决方案不管如何有用,都不是很优雅。 为此,我们可以使用方法get_items() 。 它接受两个整数:偏移量和项目计数。 这使我们能够通过传递零和零来获取所有项目,或者真正方便的是,使我们能够在所有可用的提要项目中的任何地方获取小的子集。

<?php
foreach ($feed->get_items(3, 3) as $item) {
...
}

This usage would be displaying the second page of three items.

这种用法将显示三个项目的第二页。

快取 (Caching)

With all of this data flowing, it would be quite taxing to process the entire feed every time you load the page, but don’t worry… SimplePie has you covered. There are several options for caching feed data built-in so you don’t have to pull the entire feed every time. SimplePie uses a Conditional GET to determine if a feed has been updated since the last retrieval time. To store the content, your cache storage options are the file system, MySQL, Memcache, or – if you’re feeling up to it – you can write your own handler.

有了所有这些数据,每次加载页面时都要处理整个提要是很麻烦的,但是请不要担心……SimplePie已为您介绍。 内置的供稿数据缓存有多个选项,因此您不必每次都提取整个供稿。 SimplePie使用条件GET确定自上次检索以来是否已更新feed。 要存储内容,您的缓存存储选项是文件系统,MySQL,Memcache,或者(如果您愿意)可以编写自己的处理程序。

The easiest to implement is caching to a file. Here’s how:

最容易实现的是缓存到文件。 这是如何做:

<?php
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->enable_cache();
$feed->init();

And that’s it! The newly added line turns on caching for SimplePie and writes the data out to a file. You’ll need to make sure that your document root contains a writeable directory named cache and you’re all set. If you prefer to specify a different location, that’s easy too. Simply use the method set_cache_location() and pass it a string pointing to the location you want cache files written to. The same method can also be used in the case of caching with MySQL and Memcache.

就是这样! 新添加的行打开SimplePie的缓存并将数据写出到文件中。 您需要确保文档根目录包含一个名为cache的可写目录,并且已经准备就绪。 如果您希望指定其他位置,这也很容易。 只需使用方法set_cache_location()并将其传递给一个字符串,该字符串指向您想要将缓存文件写入的位置。 在使用MySQL和Memcache进行缓存的情况下,也可以使用相同的方法。

结论 (Conclusion)

These are the most basic principles of using the SimplePie library. You’ve learned how to set up and initialize a feed, grab one or more items from the feed, and parse the information contained in those items to display.

这些是使用SimplePie库的最基本的原则。 您已经了解了如何设置和初始化提要,从提要中获取一个或多个项目以及解析这些项​​目中包含的信息以进行显示。

Of course, SimplePie’s functionality doesn’t end there. It also gives you the ability to pull items and parse information from multiple feeds at once – allowing for a mix of information displayed at your finger tips, all controlled by you. Dive into their API documentation; it’s an excellent resource and will guide you well.

当然,SimplePie的功能还不止于此。 它还使您能够一次从多个提要中提取项目并解析信息-混合显示在您的指尖上的信息,全部由您控制。 深入研究他们的API文档 ; 这是极好的资源,可以很好地指导您。

I look forward seeing the next best Google Reader replacement powered by SimplePie, written by you! You can find a brief sample to accompany this article on GitHub to get you started.

我期待看到由您撰写的由SimplePie驱动的次佳Google替代品! 您可以在GitHub上找到简短的示例,以帮助您入门。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/consuming-feeds-with-simplepie/

 类似资料: