zendframework/zend-feed
Abandoned Zend Framework package for consuming and generating RSS and Atom feeds, with a natural API for reading/modifying feed and entry elements and rendering back to XML. Moved to laminas/laminas-feed.
As noted in the previous section, you can substitute your own HTTP client by implementing the ClientInterface. In this section, we'll demonstrate doing so in order to use a client that is PSR-7-capable.
zend-feed provides a facility to assist with generating a
Zend\Feed\Reader\Response from a PSR-7 ResponseInterface via
Zend\Feed\Reader\Http\Psr7ResponseDecorator. As such, if you have a
PSR-7-capable client, you can pass the response to this decorator, and
immediately return it from your custom client:
return new Psr7ResponseDecorator($psr7Response);
We'll do this with our PSR-7 client.
Guzzle is arguably the most popular HTTP client library for PHP, and fully supports PSR-7 since version 5. Let's install it:
$ composer require guzzlehttp/guzzle
We'll use the GuzzleHttp\Client to make our requests to feeds.
From here, we'll create our client. To do this, we'll create a class that:
Zend\Feed\Reader\Http\ClientInterfaceGuzzleHttp\ClientInterface to its constructorThe code looks like this:
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Zend\Feed\Reader\Http\ClientInterface as FeedReaderHttpClientInterface;
use Zend\Feed\Reader\Http\Psr7ResponseDecorator;
class GuzzleClient implements FeedReaderHttpClientInterface
{
/**
* [@var](https://github.com/var) GuzzleClientInterface
*/
private $client;
/**
* [@param](https://github.com/param) GuzzleClientInterface|null $client
*/
public function __construct(GuzzleClientInterface $client = null)
{
$this->client = $client ?: new Client();
}
/**
* {[@inheritdoc](https://github.com/inheritdoc)}
*/
public function get($uri)
{
return new Psr7ResponseDecorator(
$this->client->request('GET', $uri)
);
}
}
In order to use our new client, we need to tell Zend\Feed\Reader\Reader about
it:
Zend\Feed\Reader\Reader::setHttpClient(new GuzzleClient());
From this point forward, this custom client will be used to retrieve feeds.
This chapter is based on a blog post by Stefan Gehrig.
How can I help you explore Laravel packages today?