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', '/');
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', '/');
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();
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
]);
You can also get the CookieJar from the response like this:
$client = new Client([
'base_uri' => 'www.google.com',
'cookies' => true,
]);
$response = $client->request('GET', '/');
$cookieJar = $response->cookies();
$cookieArray = $cookieJar->->toArray();