Guzzle 6(和Laravel 5.4)在下一个请求中重用cookie

时间:2018-09-01 23:35:27

标签: laravel session cookies guzzle cookiejar

我需要通过表单在网站上登录并从其他页面获取数据 在第二个请求中,不要设置cookie,但是我使用Guzzle包中的CookieJar

代码使用:

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$cookieJar = new CookieJar();


    $guzzle = new Client([
        'handler' => $stack,
        'debug' => true,
        'headers' => [
                'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0'
           ],
         'stream' => true,
         'connection' => 'keep-alive'
        ]);

    $request = $guzzle->post(
        'http://example.com/', [
        'form_params' => [
            'username' => 'cookiejah',
            'password' => '123456',
            'action' => 'login',
         ]
        ]
    );

    $cookie=$request->getHeader('Set-Cookie')[0];

    $request = $guzzle->get(
        'http://example.com/index.php', [
           'query' => [ 'page' => 'deposit'],
    ])->withHeader('Set-Cookie', $cookie);    

0 个答案:

没有答案