Skip to content

Commit d5bfea1

Browse files
committed
Add examples
1 parent 6c252ae commit d5bfea1

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
curl --location --cookie-jar "02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies/set/foo/bar"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$curlHandler = curl_init();
4+
5+
$cookieFile = __DIR__ . '/resource/cookie-jar.txt';
6+
7+
curl_setopt_array($curlHandler, [
8+
CURLOPT_URL => 'https://httpbin.org/cookies/set/foo/bar',
9+
CURLOPT_RETURNTRANSFER => true,
10+
11+
CURLOPT_COOKIEJAR => $cookieFile,
12+
CURLOPT_FOLLOWLOCATION => true,
13+
]);
14+
15+
$response = curl_exec($curlHandler);
16+
curl_close($curlHandler);
17+
18+
echo $response;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\RequestOptions;
7+
use GuzzleHttp\Cookie\FileCookieJar;
8+
9+
$httpClient = new Client();
10+
11+
$cookieJarFile = new FileCookieJar(
12+
__DIR__ . '/resource/guzzle-cookie-jar.json',
13+
true
14+
);
15+
16+
$response = $httpClient->get(
17+
'https://httpbin.org/cookies/set/foo/bar',
18+
[
19+
RequestOptions::COOKIES => $cookieJarFile,
20+
]
21+
);
22+
23+
echo $response->getBody()->getContents();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ For testing requests we will use the excellent services [httpbin.org](https://ht
5050
* [Send cookies from string](#send-cookies-from-string)
5151
* [Set cookie options](#set-cookie-options)
5252
* [Send cookies from file](#send-cookies-from-file)
53+
* [Save Response cookies to file](#save-response-cookies-to-file)
5354
* [Todo](#todo)
5455

5556
## Requirements
@@ -2212,6 +2213,78 @@ echo $response->getBody()->getContents();
22122213
</p>
22132214
</details>
22142215

2216+
### Save Response cookies to file
2217+
2218+
#### Bash
2219+
2220+
[[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/console.sh)]
2221+
2222+
```bash
2223+
curl --location --cookie-jar "02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/resource/cookie-jar.txt" --request GET "https://httpbin.org/cookies/set/foo/bar"
2224+
```
2225+
2226+
#### PHP CURL extension
2227+
2228+
[[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/curl-ext.php)]
2229+
2230+
```php
2231+
$curlHandler = curl_init();
2232+
2233+
$cookieFile = __DIR__ . '/resource/cookie-jar.txt';
2234+
2235+
curl_setopt_array($curlHandler, [
2236+
CURLOPT_URL => 'https://httpbin.org/cookies/set/foo/bar',
2237+
CURLOPT_RETURNTRANSFER => true,
2238+
2239+
CURLOPT_COOKIEJAR => $cookieFile,
2240+
CURLOPT_FOLLOWLOCATION => true,
2241+
]);
2242+
2243+
$response = curl_exec($curlHandler);
2244+
curl_close($curlHandler);
2245+
2246+
echo $response;
2247+
```
2248+
2249+
#### PHP Guzzle library
2250+
2251+
[[example](https://github.com/andriichuk/php-curl-cookbook/blob/master/02_Advanced/03_Cookies/04_Save_Response_Cookies_To_File/guzzle-lib.php)]
2252+
2253+
```php
2254+
use GuzzleHttp\Client;
2255+
use GuzzleHttp\RequestOptions;
2256+
use GuzzleHttp\Cookie\FileCookieJar;
2257+
2258+
$httpClient = new Client();
2259+
2260+
$cookieJarFile = new FileCookieJar(
2261+
__DIR__ . '/resource/guzzle-cookie-jar.json',
2262+
true
2263+
);
2264+
2265+
$response = $httpClient->get(
2266+
'https://httpbin.org/cookies/set/foo/bar',
2267+
[
2268+
RequestOptions::COOKIES => $cookieJarFile,
2269+
]
2270+
);
2271+
2272+
echo $response->getBody()->getContents();
2273+
```
2274+
2275+
<details><summary>Response</summary>
2276+
<p>
2277+
2278+
```json
2279+
{
2280+
"cookies": {
2281+
"foo": "bar"
2282+
}
2283+
}
2284+
```
2285+
</p>
2286+
</details>
2287+
22152288
## Todo
22162289

22172290
Project [link](https://github.com/andriichuk/php-curl-cookbook/projects/1#column-6364809)

0 commit comments

Comments
 (0)