Skip to content

Commit 4e568c9

Browse files
committed
Fill index
1 parent f3a3510 commit 4e568c9

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

index.markdown

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
layout: home
33
---
44

5+
# feed-io
56

7+
[feed-io](https://github.com/alexdebril/feed-io) is a PHP library built to consume and serve RSS / Atom feeds.
68

79
```php
810
<?php
@@ -20,3 +22,136 @@ foreach ($result->getFeed() as $item) {
2022
}
2123
```
2224

25+
# Installation
26+
27+
Use Composer to add feed-io into your project's requirements :
28+
29+
```bash
30+
composer require debril/feed-io
31+
```
32+
33+
# Requirements
34+
35+
| feed-io | PHP |
36+
| ---- | ---- |
37+
| 4.x | 7.1+ |
38+
| 5.0 | 8.0+ |
39+
40+
# Usage
41+
## CLI
42+
43+
Let's suppose you installed feed-io using Composer, you can use its command line client to read feeds from your terminal :
44+
45+
```bash
46+
./vendor/bin/feedio read http://php.net/feed.atom
47+
```
48+
49+
You can specify the number of items you want to read using the --count option. The instruction below will display the latest item :
50+
51+
```bash
52+
./vendor/bin/feedio read -c 1 http://php.net/feed.atom
53+
```
54+
55+
## Reading
56+
57+
feed-io is designed to read feeds across the internet and to publish your own. Its main class is FeedIo :
58+
59+
```php
60+
<?php
61+
// create a simple FeedIo instance
62+
$feedIo = \FeedIo\Factory::create()->getFeedIo();
63+
64+
// read a feed
65+
$result = $feedIo->read($url);
66+
67+
// get title
68+
$feedTitle = $result->getFeed()->getTitle();
69+
70+
// iterate through items
71+
foreach( $result->getFeed() as $item ) {
72+
echo $item->getTitle();
73+
}
74+
75+
```
76+
77+
## Next Update estimation
78+
79+
In order to save bandwidth, feed-io estimates the next time it will be relevant to read the feed and get new items from it.
80+
81+
```php
82+
<?php
83+
$nextUpdate = $result->getNextUpdate();
84+
echo "computed next update: {$nextUpdate->format(\DATE_ATOM)}";
85+
86+
// you may need to access the statistics
87+
$updateStats = $result->getUpdateStats();
88+
echo "average interval in seconds: {$updateStats->getAverageInterval()}";
89+
```
90+
91+
92+
feed-io calculates the next update time by first detecting if the feed was active in the last 7 days and if not we consider it as sleepy. The next update date for a sleepy feed is set to the next day at the same time. If the feed isn't sleepy we use the average interval and the median interval by adding those intervals to the feed's last modified date and compare the result to the current time. If the result is in the future, then it's returned as the next update time. If none of them are in the future, we considered the feed will be updated quite soon, so the next update time is one hour later from the moment of the calculation. Please note: the fixed delays for sleepy and closed to be updated feeds can be set through Result::getNextUpdate() arguments, see Result for more details.
93+
94+
## Formatting an object into a XML stream
95+
96+
```php
97+
<?php
98+
// build the feed
99+
$feed = new FeedIo\Feed;
100+
$feed->setTitle('...');
101+
102+
// convert it into Atom
103+
$atomString = $feedIo->toAtom($feed);
104+
105+
// or ...
106+
$atomString = $feedIo->format($feed, 'atom');
107+
```
108+
109+
### Building a feed including medias
110+
111+
```php
112+
// build the feed
113+
$feed = new FeedIo\Feed;
114+
$feed->setTitle('...');
115+
116+
$item = $feed->newItem();
117+
118+
// build the media
119+
$media = new \FeedIo\Feed\Item\Media
120+
$media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3');
121+
$media->setType('audio/mpeg');
122+
123+
// add it to the item
124+
$item->addMedia($media);
125+
126+
$feed->add($item);
127+
```
128+
129+
## Activate logging
130+
131+
feed-io natively supports PSR-3 logging, you can activate it by choosing a 'builder' in the factory :
132+
133+
```php
134+
$feedIo = \FeedIo\Factory::create(['builder' => 'monolog'])->getFeedIo();
135+
```
136+
137+
feed-io only provides a builder to create Monolog\Logger instances. You can write your own, as long as the Builder implements BuilderInterface.
138+
Building a FeedIo instance without the factory
139+
140+
To create a new FeedIo instance you only need to inject two dependencies :
141+
142+
- an HTTP Client implementing FeedIo\Adapter\ClientInterface. It can be wrapper for an external library like FeedIo\Adapter\Guzzle\Client
143+
- a PSR-3 logger implementing Psr\Log\LoggerInterface
144+
145+
```php
146+
// first dependency : the HTTP client
147+
// here we use Guzzle as a dependency for the client
148+
$guzzle = new GuzzleHttp\Client();
149+
// Guzzle is wrapped in this adapter which is a FeedIo\Adapter\ClientInterface implementation
150+
$client = new FeedIo\Adapter\Guzzle\Client($guzzle);
151+
152+
// second dependency : a PSR-3 logger
153+
$logger = new Psr\Log\NullLogger();
154+
155+
// now create FeedIo's instance
156+
$feedIo = new FeedIo\FeedIo($client, $logger);
157+
```

0 commit comments

Comments
 (0)