Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve cookies from Guzzle client?

Tags:

php

guzzle

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', '/');
like image 996
Chris Stryczynski Avatar asked Jul 10 '17 22:07

Chris Stryczynski


People also ask

How do I get guzzle response?

As described earlier, you can get the body of a response using the getBody() method. Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects. You can use the xml() method when working with XML data.

How do I send HTTP request using guzzle?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

What is GuzzleHttp client?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...


1 Answers

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();
like image 191
Alexey Shokov Avatar answered Sep 19 '22 10:09

Alexey Shokov