Setting up cookies for Guzzle CookieJar
Asked Answered
A

3

10

I am doing unit testing in PHP for a site that requires authentication. Authentication is cookie based, so I need to be able to put a cookie like this in the cookie jar:

[ 'user_token' => '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' ] 

The web application can then use this known good token for the testing data, and will be able to authenticate under testing conditions to interact with the data fixtures.

Also, it must be a secure cookie, and I (obviously) need to set the domain.

Problem is: I don't know how to make and set this cookie and stick it in the jar. How do you do that?

Afrikaner answered 5/2, 2017 at 21:7 Comment(3)
are you thinking of storing the jar in a file and reusing them??Atmosphere
Maybe. The same authentication token can be used for multiple requests as the unit test progresses, so that would be fine.Afrikaner
Few grammar & spelling fixes.Gargantua
A
20

The source code provided the answer I needed.

The CookieJar class provides a method for building cookies from an associative array. Example:

$domain = 'example.org';
$values = ['users_token' => '2c26b46b68ffc68ff99b453c1d30113413422d706483bfa0f98a5e886266e7ae'];

$cookieJar = \GuzzleHttp\Cookie\CookieJar::fromArray($values, $domain);

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://example.org',
    'cookies'  => $cookieJar 
]);
Afrikaner answered 5/2, 2017 at 22:29 Comment(0)
A
7

Simple example. this code is saving cookie in a file and loading it back next time you execute the script

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\FileCookieJar;

// file to store cookie data
$cookieFile = 'cookie_jar.txt';

$cookieJar = new FileCookieJar($cookieFile, TRUE);

$client = new Client([ 
  'base_uri' => 'http://example.com',
  // specify the cookie jar
  'cookies' => $cookieJar
]);

// guzzle/cookie.php, a page that returns cookies.
$response = $client->request('GET', 'simple-page.php');

session cookies are not stored automatically. To store the php session cookie we must set the second parameter to TRUE.

$cookieJar = new FileCookieJar($cookieFile, TRUE);

Reference

http://www.ryanwright.me/cookbook/guzzle/cookie

Atmosphere answered 5/2, 2017 at 21:19 Comment(3)
You didn't actually read my question. The part "I need to put cookies in the Jar" means I need to put values in it. see my answer below.Afrikaner
first let the file be created. then just change the tokenAtmosphere
Not a good solution. Especially when the class has a specific method for doing exactly what I wanted (in the answer to my own question, above).Afrikaner
B
0

I was lookin for a solution, I needed a way to collect cookies on redirects while starting with my own set of cookies with multi-domain support.


$cookies = [
   [
      'Name' => 'users_token', 
      'Value' => '2c26b46b68ffc68ff99b453c1d30113413422d706483bfa0f98a', 
      'Domain' => 'domain.com'
   ],
   [
      'Name' => 'session_id', 
      'Value' => '8ff99b453c1d3011341342', 
      'Domain' => 'domain.com'
   ],
   [
      'Name' => 'session_id', 
      'Value' => '8ff99b453c1d3011341342', 
      'Domain' => 'domain.us'
   ],
];

$jar = new \GuzzleHttp\Cookie\CookieJar(false, $cookies);

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://example.org',
    'cookies'  => $jar 
]);

I didn't find documentation for it, I have found the optional parameters of the constructor and decided to give it a go. Not sure if it's a good practice but it worked on my case.

Using Guzzle 7.4

Bilateral answered 23/11, 2022 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.