PHP code example of drewlabs / psr18

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

    

drewlabs / psr18 example snippets


use Drewlabs\Psr18\Client;

// Creates an instance of the cURL client
$client = Client::new(/* Parameters */);

// Passing constructor parameters
$client = Client::new('http:://127.0.0.1:5000');

// Passing customize client options
$client = Client::new([
    /* Custom client options */
]);

use Drewlabs\Curl\ClientOptions;

//
$clientOptions = ClientOptions::create([
    'verify' => false,
    'sink' => null,
    'force_resolve_ip' => false,
    'proxy' => ['http://proxy.app-ip.com'],
    'cert' => null,
    'ssl_key' => ['/home/webhost/.ssh/pub.key'],
    'progress' => new class {
        // Declare the function to handle the progress event
        public function __invoke()
        {
            // Handle the progress event
        }
    },
    'base_url' => 'http://127.0.0.1:3000',
    'connect_timeout' => 1000,
    'request' => [
        'headers' => [
            'Content-Type' => 'application/json'
        ],
        'timeout' => 10,
        'auth' => ['MyUser', 'MyPassword', 'digest'],
        'query' => [
            'post_id' => 2, 'comments_count' => 1
        ],
        'encoding' => 'gzip,deflate'
    ],
    'cookies' => [
        'clientid' => 'myClientID', 'clientsecret' => 'MySuperSecret'
    ],
]);

use Drewlabs\Curl\ClientOptions;
use Drewlabs\Curl\RequestOptions;

$clientOptions = new ClientOptions;

$clientOptions->setBaseURL(/* base url*/)
    ->setRequest(RequestOptions::create([]))
    ->setConnectTimeout(150)
    ->setVerify(false)
    // Psr Stream to write response output to
    ->setSink()
    ->setForceResolveIp(true)
    ->setProxy($proxy_ip, [$proxy_port, $user, $password]) // port, user & password are optional depending on the proxy configuration
    ->setCert('/path/to/ssl/certificate');
    ->setSslKey('/path/to/ssl/key')
    ->setProgress(function($curl, ...$progress) {
        // Handle cURL progress event
    })
    ->setCookies([]); // List of request cookies


use Drewlabs\Psr18\Client;
use Drewlabs\Psr7\Request;

// Creates an instance of the cURL client
$client = Client::new([
    // Parameters to client options ...
        'base_url' => 'http://127.0.0.1:3000',
    'connect_timeout' => 1000,
    'request' => [
        'headers' => [
            'Content-Type' => 'application/json'
        ],
        'timeout' => 10,
        'auth' => ['MyUser', 'MyPassword', 'digest'],
        'query' => [
            'post_id' => 2, 'comments_count' => 1
        ],
        'encoding' => 'gzip,deflate'
    ],
]);

$response = $client->sendRequest(new Request()); // \Psr7\Http\ResponseInterface

use Drewlabs\Psr18\Client;

// Creates an instance of the cURL client
$client = new Client(/* Parameters */);

// Sends a request with application/json as Content-Type
$client->json()->sendRequest(/* PSR7 compatible Request */);

use Drewlabs\Psr18\Client;

// Creates an instance of the cURL client
$client = new Client(
        'verify' => false,
        'request' => [
            'headers' => ['Accept' => 'application/json'],
            'body' => [ /*...*/ ],
        ],
);

// Sends a request with application/json as Content-Type
$client->multipart()->sendRequest(new Request());