-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
99 lines (91 loc) · 3.33 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* @package HTTP Client
* @link https://github.com/localzet/HttpClient
*
* @author Ivan Zorin <[email protected]>
* @copyright Copyright (c) 2018-2024 Zorin Projects S.P.
* @license https://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For any questions, please contact <[email protected]>
*/
use localzet\HTTP\Client;
require __DIR__ . '/vendor/autoload.php';
$options = [
'max_conn_per_addr' => 128, // Максимум одновременных запросов к одному URL
'keepalive_timeout' => 15, // Время жизни соединения
'connect_timeout' => 30, // Ожидание между соединениями
'timeout' => 30, // Ожидание между запросами
];
$http = new Client($options);
/**
* $http->get() принимает 3 аргумента:
* 1. URL (параметры указываются в самом URL)
* 2. Callback при удачном запросе
* 3. Callback при ошибке
*/
$http->get('https://example.com/',
success: function ($response) {
var_dump($response->getStatusCode());
echo $response->getBody();
},
error: function ($exception) {
echo $exception;
}
);
/**
* $http->post() принимает 4 аргумента:
* 1. URL
* 2. Параметры
* 3. Callback при удачном запросе
* 4. Callback при ошибке
*/
$http->post('https://example.com/', ['key1' => 'value1', 'key2' => 'value2'],
success: function ($response) {
var_dump($response->getStatusCode());
echo $response->getBody();
},
error: function ($exception) {
echo $exception;
}
);
/**
* $http->request() принимает аргументы:
* 1. URL
* 2. method, Метод
* 3. data, Параметры (вне зависимости от метода, работает http_build_query())
* 4. headers, Массив заголовков
* 5. options [
* version, Версия HTTP
* success, Callback при удачном запросе
* error Callback при ошибке
* ]
*/
$http->request(
'https://example.com/',
'POST',
['key1' => 'value1', 'key2' => 'value2'],
['Connection' => 'keep-alive'],
[
'version' => '1.1',
'success' => function ($response) {
echo $response->getBody();
},
'error' => function ($exception) {
echo $exception;
}
]
);