Skip to content

Commit 2964270

Browse files
committed
Add 'Set cookie attributes' receipt
1 parent 639bf9b commit 2964270

File tree

3 files changed

+128
-18
lines changed

3 files changed

+128
-18
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Cookie\CookieJar;
7+
use GuzzleHttp\Cookie\SetCookie;
8+
use GuzzleHttp\RequestOptions;
9+
10+
$httpClient = new Client();
11+
12+
$url = 'https://httpbin.org/cookies';
13+
$urlHost = parse_url($url, PHP_URL_HOST);
14+
15+
$cookies = [
16+
new SetCookie([
17+
'Name' => 'foo',
18+
'Value' => 'bar',
19+
'Domain' => $urlHost,
20+
'Path' => '/',
21+
'Max-Age' => 100,
22+
'Secure' => false,
23+
'Discard' => false,
24+
'HttpOnly' => false,
25+
]),
26+
new SetCookie([
27+
'Name' => 'baz',
28+
'Value' => 'foo',
29+
'Domain' => $urlHost,
30+
'Path' => '/',
31+
'Max-Age' => 100,
32+
'Secure' => false,
33+
'Discard' => false,
34+
'HttpOnly' => false,
35+
]),
36+
];
37+
38+
$cookieJar = new CookieJar(true, $cookies);
39+
40+
$response = $httpClient->get(
41+
$url,
42+
[
43+
RequestOptions::COOKIES => $cookieJar,
44+
]
45+
);
46+
47+
echo($response->getBody()->getContents());

README.md

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ For testing requests we will use the excellent services [httpbin.org](https://ht
4848
* [Bearer Auth](#bearer-auth)
4949
* [Cookies](#cookies)
5050
* [Send cookies from string](#send-cookies-from-string)
51+
* [Set cookie options](#set-cookie-options)
5152
* [Todo](#todo)
5253

5354
## Requirements
@@ -2021,23 +2022,84 @@ echo($response->getBody()->getContents());
20212022
</p>
20222023
</details>
20232024

2025+
### Set cookie options
2026+
2027+
Cookies have attributes that can be sent from the server to the client, but not from the client to the server.
2028+
The client can only send the name and value of the cookie.
2029+
2030+
#### PHP Guzzle library
2031+
2032+
[[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/02_Set_Cookie_Options/guzzle-lib.php)]
2033+
2034+
```php
2035+
use GuzzleHttp\Client;
2036+
use GuzzleHttp\Cookie\CookieJar;
2037+
use GuzzleHttp\Cookie\SetCookie;
2038+
use GuzzleHttp\RequestOptions;
2039+
2040+
$httpClient = new Client();
2041+
2042+
$url = 'https://httpbin.org/cookies';
2043+
$urlHost = parse_url($url, PHP_URL_HOST);
2044+
2045+
$cookies = [
2046+
new SetCookie([
2047+
'Name' => 'foo',
2048+
'Value' => 'bar',
2049+
2050+
// Other attributes will not be sent to the server, they are only needed for validation.
2051+
'Domain' => $urlHost,
2052+
'Path' => '/',
2053+
'Max-Age' => 100,
2054+
'Secure' => false,
2055+
'Discard' => false,
2056+
'HttpOnly' => false,
2057+
]),
2058+
new SetCookie([
2059+
'Name' => 'baz',
2060+
'Value' => 'foo',
2061+
'Domain' => $urlHost,
2062+
'Path' => '/',
2063+
'Max-Age' => 100,
2064+
'Secure' => false,
2065+
'Discard' => false,
2066+
'HttpOnly' => false,
2067+
]),
2068+
];
2069+
2070+
$cookieJar = new CookieJar(true, $cookies);
2071+
2072+
$response = $httpClient->get(
2073+
$url,
2074+
[
2075+
RequestOptions::COOKIES => $cookieJar,
2076+
]
2077+
);
2078+
2079+
echo($response->getBody()->getContents());
2080+
```
2081+
2082+
#### Request headers
2083+
2084+
```plain
2085+
> GET /cookies HTTP/1.1
2086+
Host: httpbin.org
2087+
User-Agent: GuzzleHttp/6.3.3 curl/7.58.0 PHP/7.3.6-1+ubuntu18.04.1+deb.sury.org+1
2088+
Cookie: foo=bar; baz=foo
2089+
```
2090+
2091+
<details><summary>Response</summary>
2092+
<p>
2093+
2094+
```json
2095+
{
2096+
"cookies": {
2097+
"baz": "foo",
2098+
"foo": "bar"
2099+
}
2100+
}
2101+
```
2102+
20242103
## Todo
20252104

2026-
- [x] Set HTTP version
2027-
- [x] Get cURL version
2028-
- [x] Set User agent
2029-
- [x] HTTP Referer
2030-
- [ ] Cache control
2031-
- [ ] HTTP methods (HEAD, CONNECT, OPTIONS, TRACE)
2032-
- [ ] Cookies
2033-
- [ ] Proxy
2034-
- [ ] Transfer progress
2035-
- [ ] Upload array of files in one POST field
2036-
- [ ] Upload/Download large files
2037-
- [ ] FTP transfer
2038-
- [ ] All types of Auth
2039-
- [ ] Multiple cURL handlers
2040-
- [ ] SSL certificates
2041-
- [ ] Streams
2042-
- [ ] SOAP request
2043-
- [ ] Best practices
2105+
Project link - [https://github.com/andriichuk/php-curl-cookbook/projects/1#column-6364809](https://github.com/andriichuk/php-curl-cookbook/projects/1#column-6364809)

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"ext-curl": "*",
44
"ext-mbstring": "*",
55
"ext-fileinfo": "*",
6+
"ext-json": "*",
67
"guzzlehttp/guzzle": "^6.3"
78
}
89
}

0 commit comments

Comments
 (0)