PHP code example of johnpbloch / fluent-http-api

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

    

johnpbloch / fluent-http-api example snippets


// Custom Endpoint:

class MyEndpoint extends \JohnPBloch\FluentApi\Endpoint
{
    public static function setConfig(\JohnPBloch\FluentApi\Config $config): \JohnPBloch\FluentApi\Config
    {
        return self::$config = $config;
    }

    protected function getConfig() : \JohnPBloch\FluentApi\Config {
        return self::$config;
    }
}

// Custom Config

class MyConfig extends \JohnPBloch\FluentApi\Config
{
    protected function setConfigOnBaseEndpoint(): static
    {
        return MyEndpoint::setConfig($this);
    }
}

use JohnPBloch\FluentApi\Auth\BasicAuth;
$auth = new BasicAuth('username', 'password');

use JohnPBloch\FluentApi\Auth\DigestAuth;
$auth = new DigestAuth('username', 'password', 'digest');

use JohnPBloch\FluentApi\Auth\NtlmAuth;
$auth = new NtlmAuth('username', 'password', 'ntlm');

use JohnPBloch\FluentApi\Auth\BearerAuth;
$auth = new BearerAuth('token');

use JohnPBloch\FluentApi\Auth\HeaderAuth;
$auth = new HeaderAuth('X-Header-Name', 'header value');

use JohnPBloch\FluentApi\Auth\QueryVariableAuth;
$auth = new QueryVariableAuth('query_var_name', 'token');

use JohnPBloch\FluentApi\Auth\CookieAuth;
$auth = new CookieAuth('cookie-name', 'cookie-value', 'domain.com');

use JohnPBloch\FluentApi\Config;

Config::initialize('https://api.some-domain.com/base-path/', $auth)

use JohnPBloch\FluentApi\Endpoint;

$response = Endpoint::make()
    ->method('GET')
    ->path('resource/path')
    ->send();

use JohnPBloch\FluentApi\Endpoint;

$response = Endpoint::make()
    ->method('POST')
    ->path('article')
    ->title('This Article Will Change Your Life')
    ->content('<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque commodo lacus et justo dictum, in imperdiet metus sagittis. Integer vestibulum justo quis tortor venenatis, a tempus justo pulvinar. Duis feugiat id orci ac condimentum. Fusce pellentesque dapibus tempus. Nulla nisi turpis, luctus sit amet enim sed, vulputate malesuada erat. Quisque varius quam eget sapien lobortis, ut vulputate nisl elementum. Proin sollicitudin eu ipsum vel mattis.</p>')
    ->send();