wilkques/http-client

v4.1.0 2023-08-08 08:49 UTC

This package is auto-updated.

Last update: 2024-10-08 11:31:26 UTC


README

Latest Stable Version License

How to start

composer require wilkques/http-client

How to use

use Wilkques\Http\Http;

Methods

  1. withHeaders

    $response = Http::withHeaders([ ... ]); // add header
    
    // Ex
    
    $response = Http::withHeaders([ CURLOPT_TIMEOUT => 100 ]);
  2. asForm

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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