PHP code example of wilkques / http-client

1. Go to this page and download the library: Download wilkques/http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wilkques / http-client example snippets


use Wilkques\Http\Http;

    $response = Http::withHeaders([ ... ]); // add header

    // Ex

    $response = Http::withHeaders([ CURLOPT_TIMEOUT => 100 ]);
    

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

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

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

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

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

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

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

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

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

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

    $response->body(); // get body
    

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

    $response->headers(); //get headers
    

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

    $response->ok(); // bool
    

    $response->redirect(); // bool
    

    $response->successful(); // bool
    

    $response->failed(); // bool
    

    $response->clientError(); // bool
    

    $response->serverError(); // bool
    

    $response->throw(); // throw exception
    
    // or
    
    $response->throw(new \Exception('<message>', '<code>'));

    // or

    $response->throw(function ($response, $exception) {
        // code
        // return exception
    });
    

    $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 ...