unofficialmc2 / http-client
Client HTTP utilisant CUrl
Installs: 1 033
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- ext-curl: *
- ext-json: *
- ext-openssl: *
- psr/log: ^3.0
Requires (Dev)
- monolog/monolog: ^3.0
- ocramius/package-versions: ^2.1
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^11.3
- squizlabs/php_codesniffer: ^3.7
- symfony/process: ^7.1
README
Client HTTP utilisant CUrl
Installation
composer require unofficialmc2/http-client
Utilisation
HttpClient
Requête synchrone
<?php $logger = new \Monolog\Logger('test'); $client = new \HttpClient\HttpClient($logger); $response = $client->curlUnique('https://exemple.net/path', \HttpClient\HttpMethod::GET, []); $response->isSuccess(); // true $response->isRedirect(); // false $response->isCode(404); // false $response->getHeaders(); // [ ... ] $response->getHeader('type-content'); // 'application/json' $response->getData(); // ['message' => 'lorem ipsum'] $response->getData(true); // ['message' => 'lorem ipsum'] $response->getData(false); // {"message": "lorem ipsum"}
Requête asynchrone
<?php $logger = new \Monolog\Logger('test'); $client = new \HttpClient\HttpClient($logger); $refRequest1 = $client->addParamRequest('https://exemple.net/info', \HttpClient\HttpMethod::GET, []); $refRequest2 = $client->addParamRequest('https://exemple.com/info', \HttpClient\HttpMethod::GET, []); $client->execAll(); $client->waitResult(); $response = $client->getResult($refRequest1); $response->isSuccess(); // true $response = $client->getResult($refRequest2); $response->isSuccess(); // true
HttpClient
Stub
Requête synchrone
<?php $client = new \HttpClientStub\HttpClient(); $client->addResult( 200, ['type-content'=>'application/json'], ['message' => 'lorem ipsum'] ); $response = $client->curlUnique('https://exemple.net/path', \HttpClient\HttpMethod::GET, []); $response->isSuccess(); // true
On peut enregistrer plusieures résultats pour un client. Les résultats seront retournés tour à tour en boucle.
Requête asynchrone
<?php $client = new \HttpClientStub\HttpClient(); $client->addResult( 200, ['type-content'=>'application/json'], json_encode(['message' => 'lorem ipsum']) ); $client->addResult(false, [], ''); $refRequest1 = $client->addParamRequest('https://exemple.net/info', \HttpClient\HttpMethod::GET, []); $refRequest2 = $client->addParamRequest('https://exemple.com/info', \HttpClient\HttpMethod::GET, []); $client->execAll(); $client->waitResult(); $response = $client->getResult($refRequest2); $response->isSuccess(); // false $response = $client->getResult($refRequest1); $response->isSuccess(); // true
On peut enregistrer plusieurs résultats pour un client. Les résultats seront affectés tour à tour en boucle aux requêtes ajoutées.