29

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!

JayminLimbachiya
  • 762
  • 1
  • 10
  • 17
steve
  • 1,150
  • 4
  • 13
  • 24

4 Answers4

49
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.

Federkun
  • 30,933
  • 8
  • 62
  • 80
  • 3
    Note: the domain for the cookie jar _has_ to match the domain you are requesting against. – Andy Fleming May 03 '17 at 18:45
  • 3
    i was reading that part of the documentation inside out and upside down, I couldnt find a way to set a cookie, it only mentioned misleading "cookie jar interface".... never a part of the article that looked like your suggestion. You can send us a screenshot of it to, because I can send a screenshot of the "cookies" section it never mentioned anything like this, otherwise I wouldnt have to google this one out. – Frederick G. Sandalo Jul 02 '17 at 01:55
  • 4
    With guzzle the best documentation is the source code. – Federkun Jul 02 '17 at 05:17
  • I tried out using cookieJar but getting error like cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface – JayminLimbachiya Jan 28 '20 at 06:19
3

One more way to add a cookie to the request with Guzzle:

$url = 'https://www.example.com';
$request_options = [
  'headers' => ['Cookie' => 'COOKIE_NAME=VALUE']
];
$response = $this->httpClient->request('GET', $url, $request_options);
David Thomas
  • 2,845
  • 2
  • 21
  • 20
1

Guzzle can maintain a cookie session for you if instructed using the cookies request option. When sending a request, the cookies option must be set to an instance of GuzzleHttp\Cookie\CookieJarInterface.

// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

You can set cookies to true in a client constructor if you would like to use a shared cookie jar for all requests.

// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');

Check too the full quickstart.

insign
  • 3,407
  • 30
  • 29
0

For sending cookie with Guzzle Http in laravel you can use this sample code:

        //your address
        $address = "http://example.com/xyz";
        
        //your cookie
        $coockie = ['Cookie' => "Key=Value"];
        
        //your request
        $res = Http::withOptions([
            'headers' => $coockie
        ])->get($address);