Skip to main content

Zend Feed and Guzzle

·348 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

You may have worked with Zend Feed as a standalone component. I don’t know whether you have integrated Zend framework Feed with Guzzle as Http Client.

This post is inspired by Matthew Weier O’Phinney, who have mentioned the same on github.

Our composer.json looks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "require": {
        "guzzlehttp/guzzle": "~5.2",
        "zendframework/zend-feed": "~2.3",
        "zendframework/zend-servicemanager": "~2.3"
    },
    "autoload": {
        "psr-0": {
            "": "src/"
        }
    }
}

Zen\Feed\Reader\Reader have a method importRemoteFeed which accepts an instance of Zend\Feed\Reader\Http\ClientInterface.

The Zend\Feed\Reader\Http\ClientInterface have only one method get which returns Zend\Feed\Reader\Http\ResponseInterface.

So any http client that satisfy the interface will work. Let’s create them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
// src/GuzzleClient.php
use GuzzleHttp\Client;
use Zend\Feed\Reader\Http\ClientInterface;

class GuzzleClient implements ClientInterface
{
    protected $guzzle;

    public function __construct(Client $guzzle)
    {
        $this->guzzle = $guzzle;
    }

    public function get($uri)
    {
        $response  = $this->guzzle->get($uri);
        return new GuzzleResponse($response);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
// src/GuzzleResponse.php
use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
use Zend\Feed\Reader\Http\ClientInterface;
use Zend\Feed\Reader\Http\ResponseInterface;
use Zend\Feed\Reader\Reader;

class GuzzleResponse implements ResponseInterface
{
    protected $response;

    public function __construct(Response $response)
    {
        $this->response = $response;
    }

    public function getStatusCode()
    {
        return $this->response->getStatusCode();
    }

    public function getBody()
    {
        return (string) $this->response->getBody();
    }
}

Wiring up,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use Zend\Feed\Reader\Reader as FeedReader;

$client = new GuzzleClient(new Client());
$feed = FeedReader::importRemoteFeed('http://feeds.feedburner.com/harikt/YKAJ', $client);

echo 'The feed contains ' . $feed->count() . ' entries.' . "\n\n";
foreach ($feed as $entry) {
    echo 'Title: ' . $entry->getTitle() . "\n";
    // echo 'Description: ' . $entry->getDescription() . "\n";
    echo 'URL: ' . $entry->getLink() . "\n\n";
}

You can run from the command line.

Once PSR-7 is accepted, and if both guzzle and zend framework (zf3) is built on top of it, we may not need to do the same.

Happy PhPing.

Related

Aura.Http Request and Response

·856 words·5 mins
The Aura.Http package provide you the tool to build and send request and response. Instantiation: # The easiest way is 1 2 3 <?php $http = require 'path/to/Aura.Http/scripts/instance.php'; What it gives you is an object of Aura\Http\Manager. If you want to create manually you can look into the instance.php Building your Response # Probably you may not have bothered too much on building the http response either the framework does it for you, or until you need to send the correct response.

Conduit: Middleware for PHP

·618 words·3 mins
Long back, I happened to talk with Beau Simensen about stackphp on #auraphp channel. It was hard for me to digest when I noticed it need symfony/http-kernel and its dependencies. After a few months, I started to like the middleware approach of slim framework and wanted to push it to aura. But nothing happened there. Conduit to rescue # Conduit is a Middleware for PHP built by Matthew Weier O’Phinney lead of Zend framework. Conduit supports the current PSR-7 proposal. I believe like the many PSR’s, PSR-7 will be a revolution in the PHP world. Conduit is really a micro framework and can grow with your project.

Experimenting With Different PHP Frameworks

·765 words·4 mins
Whenever I get some time, I try to learn and experiment on different frameworks. I would like to stay away from pin pointing to the frameworks I have looked, so they don’t feel bad. Long live components # Components are awesome!. You can plug your favourite components to any system. Thank you composer. One strong preference choosing a framework was