GuzzleHttp:how can I save cookies from a POST response and use it in the next POST?

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:33:08

问题


I'm using Guzzle to login my API site, and in the moment Im login with the right credentials, I get back a cookie with a RefreshToken to send it in the next call, here is my simple (and working well) code:

$client = new Client(array(
            'cookies' => true
        ));


        $response = $client->request('POST', 'http://myapi.com/login', [
            'timeout' => 30,
            'form_params' => [
                'email' => $request->get('email'),
                'password' => $request->get('password'),
            ]
        ]);

and I get back the right response with a cookie, I can see the cookie by using:

$newCookies = $response->getHeader('set-cookie');

now, I need to use this cookie in the next calls, and I know Guzzle can save the cookie for me and send it automatically (or not) in the next call using a "CookieJar" or "SessionCookieJar", I have tried to use it but I do not see the cookie in the 'jar', here is what I have done:

$cookieJar = new SessionCookieJar('SESSION_STORAGE', true);

        $client = new Client([
          'cookies' => $cookieJar
        ]);

        $response = $client->request ....

but, when I get the cookie back from the POST, I can see it only by using:

$newCookies = $response->getHeader('set-cookie');

and its not in the cookieJar, so it won't send it in the next call.. what am I missing here?

Thank you!


回答1:


As per the the documentation here, ['cookies' => true] indicates the use of a shared cookie jar for all requests, while ['cookies' => $jar] indicated the use of a specific cookie jar ($jar) for use with client's requests/ responses. So you would need to use either:

$client = new Client(array(
    'cookies' => true
));


$response = $client->request('POST', 'http://myapi.com/login', [
    'timeout' => 30,
    'form_params' => [
        'email' => $request->get('email'),
        'password' => $request->get('password'),
    ]
]);

// and using the same client

$response = $client->request('GET', 'http://myapi.com/next-url');

// or elsewhere ...

$client = new Client(array(
    'cookies' => true
));

$response = $client->request('GET', 'http://myapi.com/next-url');

or

$jar = new CookieJar;

$client = new Client(array(
    'cookies' => $jar
));


$response = $client->request('POST', 'http://myapi.com/login', [
    'timeout' => 30,
    'form_params' => [
        'email' => $request->get('email'),
        'password' => $request->get('password'),
    ]
]);

// and using the same client

$response = $client->request('GET', 'http://myapi.com/next-url');

// or elsewhere ...

$client = new Client(array(
    'cookies' => $jar // the same $jar as above
));

$response = $client->request('GET', 'http://myapi.com/another-url');


来源:https://stackoverflow.com/questions/45406915/guzzlehttphow-can-i-save-cookies-from-a-post-response-and-use-it-in-the-next-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!