PHP code example of uzdevid / conflux-http

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

    

uzdevid / conflux-http example snippets


class GithubConfig implements UzDevid\Conflux\Http\ConfigInterface {
    public function getClient(): ClientInterface {
        return new GuzzleHttp\Client;
    }

    public function getBaseUri(): string {
        return 'https://api.github.com';
    }

    public function getDefaultHeaders(): array {
        return ['Accept' => 'application/json'];
    }
}

use UzDevid\Conflux\Http\Request\RequestInterface;
use UzDevid\Conflux\Http\Request\RequestQueryInterface;
use UzDevid\Conflux\Http\Request\RequestBodyInterface;
use UzDevid\Conflux\Http\Request\ConvertableBodyInterface;
use UzDevid\Conflux\Http\Request\Method;
use UzDevid\Conflux\Http\Parser\JsonParser;

class GetUser implements RequestInterface, RequestQueryInterface, ConvertableBodyInterface {
    use JsonParser;
    
    public function getMethod(): Method {
        return Method::GET;
    }

    public function getUrl(): string {
        return '/users/{id}';
    }

    public function getQueryParams(): array {
        return [];
    }

    public function getQueryPath(): array {
        return ['{id}' => 123];
    }

    public function convert(array $response): UserDto {
        return new UserDto($response);
    }
}

use UzDevid\Conflux\Http\ConfluxHttp;
use \Yiisoft\EventDispatcher\Dispatcher\Dispatcher;

$config = new GithubConfig();
$conflux = new ConfluxHttp($config, new RequestHandler(), new Dispatcher();
$response = $conflux->withRequest(new GetUser())->send(); // UserDto

interface RequestInterface {
    public function getMethod(): Method|string;
    public function getUrl(): string;
    public function parse(string $content): array;
}

interface RequestQueryInterface {
    public function getQueryParams(): array;
    public function getQueryPath(): array;
}

interface RequestBodyInterface {
    public function getOption(): Option; // Например, 'json', 'form_params'
    public function getBody(): array|string;
}

interface RequestHeadersInterface {
    public function getHeaders(): array;
}

interface ConvertableBodyInterface {
    public function convert(array $response): mixed;
}

class CreateUser implements RequestInterface, RequestBodyInterface {
    use JsonParser;
    
    public function getMethod(): Method {
        return Method::POST;
    }

    public function getUrl(): string {
        return '/users';
    }

    public function getOption(): Option|string {
        return 'json';
    }

    public function getBody(): array|string {
        return ['name' => 'John', 'email' => '[email protected]'];
    }
}