Guzzle cookies handling

13,187

Solution 1

You are creating a new instance of the CookiePlugin on the second request, you have to use the first one on the second (and subsequent) request as well.

$cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));

//First Request
$client = new Guzzle\Http\Client();
$client->addSubscriber($cookiePlugin);
$client->post('/login');
$client->get('/test/first');

//Second Request, same client
// No need for $cookiePlugin = new CookiePlugin(...
$client->get('/test/second');

//Third Request, new client, same cookies
$client2 = new Guzzle\Http\Client();
$client2->addSubscriber($cookiePlugin); //uses same instance
$client2->get('/test/third');

Solution 2

Current answers will work if all requests are done in the same user request. But it won't work if the user first log in, then navigate through the site and query again later the "Domain".

Here is my solution (with ArrayCookieJar()):

Login

$cookiePlugin = new CookiePlugin(new ArrayCookieJar());

//First Request
$client = new Client($domain);
$client->addSubscriber($cookiePlugin);
$request = $client->post('/login');
$response = $request->send();

// Retrieve the cookie to save it somehow
$cookiesArray = $cookiePlugin->getCookieJar()->all($domain);
$cookie = $cookiesArray[0]->toArray();

// Save in session or cache of your app.
// In example laravel:
Cache::put('cookie', $cookie, 30);

Other request

// Create a new client object
$client = new Client($domain);
// Get the previously stored cookie
// Here example for laravel
$cookie = Cache::get('cookie');
// Create the new CookiePlugin object
$cookie = new Cookie($cookie);
$cookieJar = new ArrayCookieJar();
$cookieJar->add($cookie);
$cookiePlugin = new CookiePlugin($cookieJar);
$client->addSubscriber($cookiePlugin);

// Then you can do other query with these cookie
$request = $client->get('/getData');
$response = $request->send();

Solution 3

$cookiePlugin = new CookiePlugin(new FileCookieJar($cookie_file_name));

// Add the cookie plugin to a client
$client = new Client($domain);
$client->addSubscriber($cookiePlugin);

// Send the request with no cookies and parse the returned cookies
$client->get($domain)->send();

// Send the request again, noticing that cookies are being sent
$request = $client->get($domain);
$request->send();

print_r ($request->getCookies());
Share:
13,187
Peter Krejci
Author by

Peter Krejci

PHP MySQL PostgreSQL MongoDB (Developer Certified, DBA Certified) C/C++ Linux

Updated on June 17, 2022

Comments

  • Peter Krejci
    Peter Krejci almost 2 years

    I'm building a client app based on Guzzle. I'm getting stucked with cookie handling. I'm trying to implement it using Cookie plugin but I cannot get it to work. My client application is standard web application and it looks like it's working as long as I'm using the same guzzle object, but across requests it doesn't send the right cookies. I'm using FileCookieJar for storing cookies. How can I keep cookies across multiple guzzle objects?

    // first request with login works fine
    $cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));
    $client->addSubscriber($cookiePlugin);
    
    $client->post('/login');
    
    $client->get('/test/123.php?a=b');
    
    
    // second request where I expect it working, but it's not...
    $cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));
    $client->addSubscriber($cookiePlugin);
    
    $client->get('/another-test/456');