PHP code example of thingston / psr18

1. Go to this page and download the library: Download thingston/psr18 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/ */

    

thingston / psr18 example snippets




use Thingston\Psr17\RequestFactory;
use Thingston\Psr18\Client;
use Thingston\Psr18\Curl\RequestOptions;
use Thingston\Psr18\Curl\ResponseOptions;
use Thingston\Psr18\RetryPolicy;

$request = (new RequestFactory())
    ->createRequest('POST', 'https://example.com/api')
    ->withHeader('Accept', 'application/json');

$client = new Client(
    requestOptions: (new RequestOptions())
        ->withOption(RequestOptions::TIMEOUT, 10)
        ->withOption(RequestOptions::USER_AGENT, 'thingston/http-client'),
    responseOptions: (new ResponseOptions())
        ->withOption(ResponseOptions::ENCODING, ''),
    retryPolicy: new RetryPolicy(maxRetries: 2, delayMilliseconds: 100),
);

$response = $client->sendRequest($request);

use Thingston\Psr18\Client;
use Thingston\Psr18\Curl\RequestOptions;

$client = new Client(
    requestOptions: (new RequestOptions())
        ->withOption(RequestOptions::USER_AGENT, 'thingston/http-client')
        ->withOption(RequestOptions::CONNECT_TIMEOUT, 10)
        ->withOption(RequestOptions::MAX_REDIRECTS, 10)
        ->withOption(RequestOptions::FOLLOW_LOCATION, true),
);

use Thingston\Psr18\Client;
use Thingston\Psr18\Curl\ResponseOptions;

$client = new Client(
    responseOptions: (new ResponseOptions())
        ->withOption(ResponseOptions::ENCODING, ''),
);

use Thingston\Psr18\Client;
use Thingston\Psr18\RetryPolicy;

$client = new Client(
    retryPolicy: new RetryPolicy(maxRetries: 3, delayMilliseconds: 200),
);