定制 gomes81/guzzlehttp-cookie-auth-subscriber 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gomes81/guzzlehttp-cookie-auth-subscriber

Composer 安装命令:

composer require gomes81/guzzlehttp-cookie-auth-subscriber

包简介

Guzzle Cookie Auth subscriber

README 文档

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);

统计信息

  • 总下载量: 199
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固