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)

不言
Release: 2023-04-05 17:20:02
forward
5374 people have browsed it

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);
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template