NB: outputs are stored at pre-commit time.
Tivins\Core\Net\Client)COPYuse Client as Client; use ClientException as ClientException; require 'vendor/autoload.php'; try { $client = (new Client('https://httpbin.org/anything'))->execute(); echo $client . ' ' . $client->getCode() . ' (' . strlen($client->getContent()) . ')' . PHP_EOL; print_r($client->getContentAsJSON()); } catch (Exception|ClientException $ex) { exit($ex->client . ' : ' . $ex->getMessage() . "\n"); }
Tivins\Core\Net\Client#4 200 (542)
stdClass Object
(
[args] => stdClass Object
(
)
[data] =>
[files] => stdClass Object
(
)
[form] => stdClass Object
(
)
[headers] => stdClass Object
(
[Accept] => */*
[Accept-Encoding] => deflate, gzip, br
[Content-Length] => 0
[Content-Type] => application/x-www-form-urlencoded
[Host] => httpbin.org
[User-Agent] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0
[X-Amzn-Trace-Id] => Root=1-63cee135-29c6c8a615ad07af6cc18c58
)
[json] =>
[method] => GET
[origin] => xx.xx.xx.xx
[url] => https://httpbin.org/anything
)
COPYuse Tivins\Core\Net\Client; use Tivins\Core\Net\ClientException; use Tivins\Core\Net\Http\Header; use Tivins\Core\Net\Http\Headers; require 'vendor/autoload.php'; $token = 'a-token-from-elsewhere'; $headers = (new Headers())->setHeader(Header::Authorization, 'Bearer ' . $token); try { $client = (new Client('https://httpbin.org/anything'))->setHeaders($headers)->postJSON(['yo' => 'lo'])->execute(); } catch (ClientException $e) { exit($e->client . ' ' . $e->getMessage() . "\n"); } echo $client . ' ' . $client->getCode() . ' (' . strlen($client->getContent()) . ')' . PHP_EOL; echo $client->getContent() . PHP_EOL;
Tivins\Core\Net\Client#21 200 (613)
{
"args": {},
"data": "{\"yo\":\"lo\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip, br",
"Authorization": "Bearer a-token-from-elsewhere",
"Content-Length": "11",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:101.0) Gecko/20100101 Firefox/101.0",
"X-Amzn-Trace-Id": "Root=1-63cee136-1ec8d0b936b8d70030aa3d1a"
},
"json": {
"yo": "lo"
},
"method": "POST",
"origin": "xx.xx.xx.xx",
"url": "https://httpbin.org/anything"
}
Cet exemple démontre comment effectuer une requête HTTP de manière asynchrone.
Appeler la méthode setProgressCallback() pour fournir la callback qui sera appelée durant le traitement.
COPYuse ClientAsync as ClientAsync; use ClientException as ClientException; use ClientMulti as ClientMulti; require 'vendor/autoload.php'; $client = (new ClientAsync('https://httpbin.org/anything'))->setProgressCallback( function (*todo*) use (*todo*) { echo "$client => " . number_format($duration, 1) . "s\n"; } )->postJSON(['yo' => 'lo']); try { $client->execute(); } catch (ClientException $e) { exit($e->client . ' ' . $e->getMessage() . "\n"); } echo $client . ' ' . $client->getCode() . ' (' . strlen($client->getContent()) . ')' . PHP_EOL . $client->getContent() . PHP_EOL;
Tivins\Core\Net\ClientMulti#5 => 0.0s
Tivins\Core\Net\ClientMulti#5 => 0.1s
Tivins\Core\Net\ClientMulti#5 => 0.2s
Tivins\Core\Net\ClientMulti#5 => 0.3s
Tivins\Core\Net\ClientMulti#5 => 0.4s
Tivins\Core\Net\ClientMulti#5 => 0.5s
Tivins\Core\Net\ClientMulti#5 => 0.6s
Tivins\Core\Net\ClientAsync#4 200 (556)
{
"args": {},
"data": "{\"yo\":\"lo\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip, br",
"Content-Length": "11",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0",
"X-Amzn-Trace-Id": "Root=1-63cee136-3f4a2532396f25634d25f99f"
},
"json": {
"yo": "lo"
},
"method": "POST",
"origin": "xx.xx.xx.xx",
"url": "https://httpbin.org/anything"
}
COPYuse Tivins\Core\Net\Client; use Tivins\Core\Net\ClientException; use Tivins\Core\Net\ClientMulti; require 'vendor/autoload.php'; $clients = new ClientMulti(); $clients->addClients('https://example.com/', (new Client('https://httpbin.org/anything'))->postJSON(['yo' => 'lo'])); $duration = $clients->execute(); foreach (*todo*) { printf("- #%d : %s, Code: %d, Size: %d bytes\n", $k, $client, $client->getCode(), strlen($client->getContent())); }
- #45 : Tivins\Core\Net\Client#43, Code: 200, Size: 1256 bytes - #46 : Tivins\Core\Net\Client#5, Code: 200, Size: 558 bytes
...todo.