Feeds ​
- Type
- Class
- Namespace
- craft\feeds
- Inherits
- craft\feeds\Feeds » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
- Deprecated in
- in 3.4.24
The Feeds service provides APIs for fetching remote RSS and Atom feeds.
An instance of the Feeds service is globally accessible in Craft via Craft::$app->feeds
.
Public Properties ​
Property | Description |
---|---|
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | Constructor. |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
behaviors() | Returns a list of behaviors that this component should behave as. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
getFeed() | Fetches and parses an RSS or Atom feed, and returns info about the feed and its items. |
getFeedItems() | Fetches and parses an RSS or Atom feed, and returns its items. |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
init() | Initializes the object. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
trigger() | Triggers an event. |
getFeed()
​
- Since
- 3.0.37
Fetches and parses an RSS or Atom feed, and returns info about the feed and its items.
The returned array will have the following keys:
authors
– An array of the feed’s authors, where each sub-element has the following keys:name
– The author’s nameurl
– The author’s URLemail
– The author’s email
categories
– An array of the feed’s categories, where each sub-element has the following keys:term
– The category’s termscheme
– The category’s schemelabel
– The category’s label
copyright
– The copyright info for the feed, or null uf not known.dateCreated
– The feed’s creation date, or null if not known.dateUpdated
– The feed’s last modification date, or null if not known.description
– The feed’s description, or null if not known.generator
– The feed’s generator, or null if not known.id
– The feed’s ID, or null if not known.items
– An array of the feed’s items. See getFeedItems() for a list of keys each element in this array will contain.language
– The feed’s language, or null if not known.link
– The link to the feed’s HTML source, or null if not known.title
– The feed’s title, or null if not known.
Arguments ​
$url
(string) – The feed’s URL.$cacheDuration
(mixed
, null) – How long to cache the results. See craft\helpers\ConfigHelper::durationInSeconds() for possible values.
Returns ​
array – The feed info
Throws ​
Example ​
php
$feedUrl = 'https://craftcms.com/news.rss';
$feed = Craft::$app->feeds->getFeed($feedUrl, 10);
twig
{% set feedUrl = "https://craftcms.com/news.rss" %}
{% set feed = craft.app.feeds.getFeed(feedUrl) %}
<h3>{{ feed.title }}</h3>
{% for item in feed.items[0:10] %}
<article>
<h3><a href="{{ item.permalink }}">{{ item.title }}</a></h3>
<p class="author">{{ item.authors[0].name }}</p>
<p class="date">{{ item.date|date('short') }}</p>
{{ item.summary }}
</article>
{% endfor %}
getFeedItems()
​
Fetches and parses an RSS or Atom feed, and returns its items.
Each element in the returned array will have the following keys:
authors
– An array of the item’s authors, where each sub-element has the following keys:name
– The author’s nameurl
– The author’s URLemail
– The author’s email
categories
– An array of the item’s categories, where each sub-element has the following keys:term
– The category’s termscheme
– The category’s schemelabel
– The category’s label
content
– The item’s main content.contributors
– An array of the item’s contributors, where each sub-element has the following keys:name
– The contributor’s nameurl
– The contributor’s URLemail
– The contributor’s email
date
– A\craft\feeds\DateTime
object representing the item’s date.dateUpdated
– A\craft\feeds\DateTime
object representing the item’s last updated date.permalink
– The item’s URL.summary
– The item’s summary content.title
– The item’s title.
Arguments ​
$url
(string) – The feed’s URL.$limit
(integer, null) – The maximum number of items to return. Default is 0 (no limit).$offset
(integer, null) – The number of items to skip. Defaults to 0.$cacheDuration
(mixed
, null) – How long to cache the results. See craft\helpers\ConfigHelper::durationInSeconds() for possible values.
Returns ​
array – The list of feed items.
Throws ​
Example ​
php
$feedUrl = 'https://craftcms.com/news.rss';
$items = Craft::$app->feeds->getFeedItems($feedUrl, 10);
twig
{% set feedUrl = "https://craftcms.com/news.rss" %}
{% set items = craft.app.feeds.getFeedItems(feedUrl, 10) %}
{% for item in items %}
<article>
<h3><a href="{{ item.permalink }}">{{ item.title }}</a></h3>
<p class="author">{{ item.authors[0].name }}</p>
<p class="date">{{ item.date|date('short') }}</p>
{{ item.summary }}
</article>
{% endfor %}