Search by

wilkques / http-client

wilkques

http-client

Package info

github.com/wilkques/http-client

Issues

pkg:composer/wilkques/http-client

Statistics

Installs: 369

Dependents: 2

Suggesters: 0

Stars: 0

v6.0.0 2026-09-24 08:11 UTC

This package is auto-updated.

Last update: 2026-09-24 08:25:58 UTC


README

TESTS Latest Stable Version License

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

  1. setCurlOption PHP CURL Setting

    $response = Http::setCurlOption(<CURL OPTION>, <Value>);
    
        // Ex
    
    $response = Http::setCurlOption(CURLOPT_TIMEOUT, 100);
  2. withHeaders

    $response = Http::withHeaders([ ... ]); // add header
    
    // Ex
    
    $response = Http::withHeaders([
        'Accept' => 'application/json; charset=utf-8'
    ]);
  3. setHeader

    $response = Http::setHeader('<header>', '<value>'); // add header
    
    // Ex
    
    $response = Http::setHeader('Accept', 'application/json; charset=utf-8');
  4. asForm

    $response = Http::asForm(); // add header application/x-www-form-urlencoded
  5. asJson

    $response = Http::asJson(); // add header application/json
  6. asMultipart

    $response = Http::asMultipart(); // add header multipart/form-data
  7. attach

    $response = Http::attach('<post key name>', '<file path>', '<file type>', '<file name>'); // add file
  8. withBasicAuth

    $response = Http::withBasicAuth('<username>', '<password>'); // add Basic Authorization header
  9. withDigestAuth

    $response = Http::withDigestAuth('<username>', '<password>'); // use HTTP Digest auth
  10. withCookies

    $response = Http::withCookies(['session' => '<value>']); // add Cookie header
  11. withUserAgent

    $response = Http::withUserAgent('<user agent>'); // set User-Agent header
  12. get

    $response = Http::get('<url>', [ ... ]); // Http method get
  13. post

    $response = Http::post('<url>', [ ... ]) // Http method post
  14. put

    $response = Http::put('<url>', [ ... ]) // Http method put
  15. patch

    $response = Http::patch('<url>', [ ... ]) // Http method patch
  16. delete

    $response = Http::delete('<url>', [ ... ]) // Http method delete
  17. timeout

    $response = Http::timeout(30); // total request time limit, in seconds
  18. connectTimeout

    $response = Http::connectTimeout(10); // connection-establishment time limit, in seconds
  19. withoutRedirecting

    $response = Http::withoutRedirecting()->get('<url>'); // stop at the first 3xx instead of following it
  20. maxRedirects

    $response = Http::maxRedirects(3)->get('<url>'); // follows redirects by default (up to 5); this changes the limit
  21. 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
  22. status

    $response->status(); // get http status code
  23. body

    $response->body(); // get body
  24. json

    $response->json(); // get json_decode body
  25. headers

    $response->headers(); //get headers
  26. header

    $response->header('<key>'); // get header
  27. ok

    $response->ok(); // bool
  28. redirect

    $response->redirect(); // bool
  29. successful

    $response->successful(); // bool
  30. failed

    $response->failed(); // bool
  31. clientError

    $response->clientError(); // bool
  32. serverError

    $response->serverError(); // bool
  33. throw

    $response->throwException(); // throw exception
    
    // or
    
    $response->throwException(new \Exception('<message>', '<code>'));
    
    // or
    
    $response->throwException(function ($response, $exception) {
        // code
        // return exception
    });
  34. 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;
    });
  35. 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 ...