wilkques / http-client
http-client
v6.0.0
2026-09-24 08:11 UTC
Requires
- php: >=5.3
- ext-curl: *
- ext-json: *
- wilkques/php-helper: ^6.1.0
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
English | 繁體中文
Requirements
- PHP >= 5.3 (tested against 5.3, 7.4, 8.3)
- ext-curl
How to start
composer require wilkques/http-client
How to use
use Wilkques\Http\Http;
Methods
-
setCurlOptionPHP CURL Setting$response = Http::setCurlOption(<CURL OPTION>, <Value>); // Ex $response = Http::setCurlOption(CURLOPT_TIMEOUT, 100);
-
withHeaders$response = Http::withHeaders([ ... ]); // add header // Ex $response = Http::withHeaders([ 'Accept' => 'application/json; charset=utf-8' ]);
-
setHeader$response = Http::setHeader('<header>', '<value>'); // add header // Ex $response = Http::setHeader('Accept', 'application/json; charset=utf-8');
-
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
-
withBasicAuth$response = Http::withBasicAuth('<username>', '<password>'); // add Basic Authorization header
-
withDigestAuth$response = Http::withDigestAuth('<username>', '<password>'); // use HTTP Digest auth
-
withCookies$response = Http::withCookies(['session' => '<value>']); // add Cookie header
-
withUserAgent$response = Http::withUserAgent('<user agent>'); // set User-Agent header
-
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
-
timeout$response = Http::timeout(30); // total request time limit, in seconds
-
connectTimeout$response = Http::connectTimeout(10); // connection-establishment time limit, in seconds
-
withoutRedirecting$response = Http::withoutRedirecting()->get('<url>'); // stop at the first 3xx instead of following it
-
maxRedirects$response = Http::maxRedirects(3)->get('<url>'); // follows redirects by default (up to 5); this changes the limit
-
retry// retries only on a transport-level failure (DNS/connection/timeout), // not on an HTTP error status like 4xx/5xx $response = Http::retry(3, 100)->get('<url>'); // up to 3 attempts, 100ms between attempts
-
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->throwException(); // throw exception // or $response->throwException(new \Exception('<message>', '<code>')); // or $response->throwException(function ($response, $exception) { // code // return exception });
-
throwIf/throwUnless// throws regardless of failed()/successful() — purely off the given // condition (bool, or callable(Response): bool) $response->throwIf($response->header('X-Foo') === null); $response->throwUnless(function ($response) { return $response->header('X-Foo') !== null; });
-
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->alias('get')->get('http://example.com/get', ['ghi' => 789]), $pool->alias('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 ...