Setting Cookies for Guzzle CookieJar

I am doing unit testing in PHP for a site that requires authentication. Authentication is cookie based, so I need to add cookies to the cookie doll:

[ 'user_token' => '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' ] 

Then the web application can use this well-known good token for test data and will be able to authenticate in the test environment to interact with the data.

In addition, it must be a secure cookie, and I (obviously) must set the domain.

The problem is that I do not know how to make and set this cookie and paste it into the jar. How do you do this?

+4
source share
2 answers

.

CookieJar cookie . :

$jar = new \GuzzleHttp\Cookie\CookieJar;

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

$cookieJar = $jar->fromArray($values,$domain);

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

. cookie 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');

cookie . php session cookie, TRUE.

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

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

+2

Source: https://habr.com/ru/post/1668883/


All Articles