wilkques / http-client
http-client
v4.1.0
2023-08-08 08:49 UTC
Requires
- php: >=7.0
- ext-curl: *
- ext-json: *
- wilkques/php-helper: ^3.0.0
README
How to start
composer require wilkques/http-client
How to use
use Wilkques\Http\Http;
Methods
-
withHeaders
$response = Http::withHeaders([ ... ]); // add header // Ex $response = Http::withHeaders([ CURLOPT_TIMEOUT => 100 ]);
-
asForm
$response = Http::asForm(); // add header application/x-www-form-urlencoded
-
asJson
$response = Http::asJson(); // add header application/json
-
asMultipart
$response = Http::asMultipart(); // add header multipart/form-data
-
attach
$response = Http::attach('<post key name>', '<file path>', '<file type>', '<file name>'); // add file
-
get
$response = Http::get('<url>', [ ... ]); // Http method get
-
post
$response = Http::post('<url>', [ ... ]) // Http method post
-
put
$response = Http::put('<url>', [ ... ]) // Http method put
-
patch
$response = Http::patch('<url>', [ ... ]) // Http method patch
-
delete
$response = Http::delete('<url>', [ ... ]) // Http method delete
-
status
$response->status(); // get http status code
-
body
$response->body(); // get body
-
json
$response->json(); // get json_decode body
-
headers
$response->headers(); //get headers
-
header
$response->header('<key>'); // get header
-
ok
$response->ok(); // bool
-
redirect
$response->redirect(); // bool
-
successful
$response->successful(); // bool
-
failed
$response->failed(); // bool
-
clientError
$response->clientError(); // bool
-
serverError
$response->serverError(); // bool
-
throw
$response->throw(); // throw exception // or $response->throw(new \Exception('<message>', '<code>')); // or $response->throw(function ($response, $exception) { // code // return exception });
-
pool
$response = \Wilkques\Http\Http::Pool(function (\Wilkques\Http\Pool $pool) { return [ $pool->get('http://example.com/get', ['abc' => 123]), $pool->post('http://example.com/post', ['def' => 456]), $pool->as('get')->get('http://example.com/get', ['ghi' => 789]), $pool->as('post')->post('http://example.com/post', ['jkl' => 012]), ]; }, [ 'response' => [ 'sort' => true, // response sort, default true ], 'timeout' => 100, // timeout microseconds suggest < 1 sec, default 100 // success 'fulfilled' => function (\Wilkques\Http\Response $response, $index) { var_dump($index); // array index var_dump($response); // \Wilkques\Http\Response return $response; }, // fail 'rejected' => function (\Wilkques\Http\Exceptions\CurlExecutionException $exception, $index) { var_dump($index); // array index var_dump($exception); // \Wilkques\Http\Exceptions\CurlExecutionException return $response; }, 'options' => [ // curl_multi_setopt option & value ... ] ]); // output // array( // '0' => Wilkques\Http\Response..., // '1' => Wilkques\Http\Response..., // 'get' => Wilkques\Http\Response..., // 'post' => Wilkques\Http\Response..., // ) var_dump($response); $response[0]->failed(); $response[1]->successful(); // etc ...