@@ -48,6 +48,7 @@ For testing requests we will use the excellent services [httpbin.org](https://ht
48
48
* [ Bearer Auth] ( #bearer-auth )
49
49
* [ Cookies] ( #cookies )
50
50
* [ Send cookies from string] ( #send-cookies-from-string )
51
+ * [ Set cookie options] ( #set-cookie-options )
51
52
* [ Todo] ( #todo )
52
53
53
54
## Requirements
@@ -2021,23 +2022,84 @@ echo($response->getBody()->getContents());
2021
2022
</p >
2022
2023
</details >
2023
2024
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
+
2024
2103
## Todo
2025
2104
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 )
0 commit comments