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 theCookie
header in the request with the cookiesfoo=bar
andbar=baz
.$response = $client->request('GET', 'http://httpbin.org/cookies', [ ... ])
- This sends the request to the specified URL with theCookie
header set.
Helpful links
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- What version of PHP is required for Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to set a timeout for a request with PHP Guzzle?
- How to add a bearer token in PHP Guzzle?
- How to add an authorization header bearer in PHP Guzzle?
- How to update PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
See more codes...