Home >Backend Development >PHP Tutorial >Introduction to guzzlehttp's method of passing custom cookies (with code)

Introduction to guzzlehttp's method of passing custom cookies (with code)

不言
不言forward
2019-03-21 15:49:055403browse

This article brings you an introduction to the method of passing custom cookies through guzzlehttp (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

guzzlehttp is a very powerful http client package in PHP language, which supports saving the cookie information returned by the server in previous requests for subsequent requests.

In actual development, we often need to pass customized cookie values. This function has not been introduced in the official documents. I searched online for a long time but could not find the relevant implementation. Finally, I found it on a foreign website. Here is an example, I hope it will be helpful to others.

$default = [
    'cookies' =>[
        'x-token' => "test-token",
        'appId' => "test-appid"
    ],
    'headers'=>[]
];
$cookie && $default['cookies'] = array_merge($default['cookies'], **$cookie**);
$cookieJar = CookieJar::fromArray($default['cookies'],  $this->domain);

$client = new Client();
$options = [
    'form_params' => $param,
    'headers' => $headers,
    'cookies' => $cookieJar
];
$response = $client->post($this->baseURL.$path, $options);

The above is the detailed content of Introduction to guzzlehttp's method of passing custom cookies (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete