Reputation: 33921
How can I retrieve the cookies from a Guzzle request / client, after a request has occurred?
$client = new Client([
'base_uri' => 'www.google.com',
]);
$response = $client->request('GET', '/');
Upvotes: 15
Views: 26311
Reputation: 1860
With Guzzle v7, you can use this:
$client = new \GuzzleHttp\Client();
$jar = new \GuzzleHttp\Cookie\CookieJar();
$client->get('https://www.google.com', ['cookies' => $jar])->getBody()->getContents();
var_dump($jar);
// second request with same cookie jar
$client->get('https://www.google.com', ['cookies' => $jar])->getBody()->getContents();
The docs are here
Upvotes: 0
Reputation: 321
just updating this post.
In Guzzle version 8.0, the getConfig()
method will be @deprecated
. Read the docs.
You can get cookies, from sample code:
$client = new Client([
'base_uri' => 'YOUR_URI',
]);
$response = $client->post('PATH');
$headerSetCookies = $response->getHeader('Set-Cookie');
$cookies = [];
foreach ($headerSetCookies as $key => $header) {
$cookie = SetCookie::fromString($header);
$cookie->setDomain('YOUR_DOMAIN');
$cookies[] = $cookie;
}
$cookieJar = new CookieJar(false, $cookies);
For use, then:
$client->post('PATH', [
'cookies' => $cookieJar
]);
Upvotes: 5
Reputation: 5010
Read the docs, please. You have to use CookieJar
class to work with cookies.
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');
$cookieJar = $client->getConfig('cookies');
$cookieJar->toArray();
Upvotes: 34