How can I send Cookies with Guzzlehttp/guzzle 6?

steve picture steve · Dec 6, 2016 · Viewed 28.2k times · Source

I need to send a request with custom cookies.

I have tried to set cookieJar like this:

$cookieJar = CookieJar::fromArray(array($cookieName=>$cookieStr),
                    'api.mobra.in');

                $res = $this->guzzleClient->request($requestMethod, $url,
                    [
                        'cookies' => [$cookieJar]
                    ]
                );

But it is getting an error

cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface

Please suggest example or explain in details. I gone through documents but they have not mentioned in detail.

Thank you!

Answer

Federkun picture Federkun · Dec 6, 2016
use GuzzleHttp\Cookie\CookieJar;

$cookieJar = CookieJar::fromArray([
    'cookie_name' => 'cookie_value'
], 'example.com');

$client->request('GET', '/get', ['cookies' => $cookieJar]);

You can read the documentation here.