gomes81 / guzzlehttp-cookie-auth-subscriber
Guzzle Cookie Auth subscriber
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-11-26 08:20:30 UTC
README
Signs HTTP requests using cookies. Requests are signed using a login form, containing a username and password (and / or any other fields you wish to include). The credentials are then send to the provided url using the specified HTTP method (usually POST) and the returned cookies are saved for later use.
This version only works with Guzzle 6.0 and up!
Installing
This project can be installed using Composer. Add the following to your composer.json:
{ "require": { "gomes81/guzzlehttp-cookie-auth-subscriber": "0.1.*" } }
Using the Subscriber
Here's an example showing how to send an authenticated request:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Gomes81\GuzzleHttp\Subscriber\CookieAuth; $stack = HandlerStack::create(); $middleware = new CookieAuth( '/login_simple_url_or_path', [ 'username' => 'my_username', 'password' => 'my_password', 'other_field' => 'my_field_value' ], 'POST',// GET, POST or JSON (will JSON encode fields array and send it inside the POST request body) 'you can also pass a cookie string to use in here'); $stack->push($middleware); $client = new Client([ 'base_uri' => 'http://simple_url.com', 'handler' => $stack ]); // Set the "auth" request option to "cookie" to sign the request using a cookie // Before calling the given url the subscriber will check if there's a valid cookie // to be injected in the current request, if a valid cookie could not be found an // additional request is made to obtain it $res = $client->get('statuses/home_timeline.json', ['auth' => 'cookie']);
You can set the auth
request option to cookie
for all requests sent by
the client by extending the array you feed to new Client
with auth => cookie.
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Gomes81\GuzzleHttp\Subscriber\CookieAuth; $stack = HandlerStack::create(); $middleware = new CookieAuth( '/login_simple_url_or_path', [ 'username' => 'my_username', 'password' => 'my_password', 'other_field' => 'my_field_value'], 'POST',// GET, POST or JSON (will JSON encode fields array and send it inside the POST request body) 'you can also pass a cookie string to use in here'); $stack->push($middleware); $client = new Client([ 'base_uri' => 'http://simple_url.com', 'handler' => $stack, 'auth' => 'cookie' ]); // Now you don't need to add the auth parameter $res = $client->get('statuses/home_timeline.json');
You can also save cookies to a file by passing a FileCookieJar object instance in the forth parameter.
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Cookie\FileCookieJar; use Gomes81\GuzzleHttp\Subscriber\CookieAuth; $stack = HandlerStack::create(); $middleware = new CookieAuth( '/login_simple_url_or_path', [ 'username' => 'my_username', 'password' => 'my_password', 'other_field' => 'my_field_value'], 'POST',// GET, POST or JSON (will JSON encode fields array and send it inside the POST request body) new FileCookieJar('./cookies_folder/cookie_file_name', true)); $stack->push($middleware);