Giter Club home page Giter Club logo

expressive-guzzle-cookiejar's Introduction

expressive-guzzle-cookiejar

Build Status Coverage Status

A Guzzle cookiejar implementation with zend-expressive-session persistence.

If your Expressive application uses Guzzle and you need to persist the cookies Guzzle receives between requests to your application, this package is for you. It's particularly useful if you are accessing an API endpoint that uses sessions to log you in.

Installation

composer install kynx/expressive-guzzle-cookiejar

Usage

You will need Zend\Expressive\Session\SessionMiddleware piped into your application before the handler that is using Guzzle so that the session is available in the request.

The following illustrates a simple proxy for an imaginary REST API:

<?php

namespace My\Handler;

use GuzzleHttp\ClientInterface;
use Kynx\Guzzle\Expressive\ExpressiveCookieJar;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class MyProxy implements RequestHandlerInterface
{
    private $client;
    private $username;
    private $password;
    
    public function __construct(ClientInterface $client, string $username, string $password) 
    {
        $this->client = $client;
        $this->username = $username;
        $this->password = $password;
    }
    
    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        // pass `true` as the third parameter so session cookies are stored
        $cookieJar = ExpressiveCookieJar::fromRequest($request, 'my-proxy', true);
        
        // do we have any cookies?
        if (! count($cookieJar)) {
            $this->login($cookieJar);
        }
        
        // proxy the request
        return $this->client->request('GET', $request->getUri()->getPath(), ['cookies' => $cookieJar]);
    }
    
    private function login(ExpressiveCookieJar $cookieJar)
    {
        $this->client->request('GET', '/some/rest/auth', [
            'auth' => [$this->username, $this->password],
            'cookies' => $cookieJar
        ]);
    }
}

OK, there will be a bit more to it than that, like handling authentication failures and session timeouts on the remote system. But not too much more :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.