php-guzzleHow to use cookies with Guzzle in PHP?
Cookies can be used with Guzzle in PHP by setting the Cookie header in the request.
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://httpbin.org/cookies', [
'headers' => [
'Cookie' => 'foo=bar; bar=baz'
]
]);
The output of the above code will be a response object containing the cookies sent in the request.
Code explanation
$client = new GuzzleHttp\Client();- This creates a new Guzzle client object.'headers' => [ 'Cookie' => 'foo=bar; bar=baz' ]- This sets theCookieheader in the request with the cookiesfoo=barandbar=baz.$response = $client->request('GET', 'http://httpbin.org/cookies', [ ... ])- This sends the request to the specified URL with theCookieheader set.
Helpful links
More of Php Guzzle
- How to send multipart requests with Guzzle in PHP?
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to install PHP Guzzle without Composer?
- How to use GraphQL with PHP Guzzle?
- How to use a proxy with Guzzle in PHP?
- How to use multipart/form-data with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to mock requests with Guzzle in PHP?
- How to log requests with Guzzle in PHP?
See more codes...