mar3q/http-client-php-example

0.1.0 2023-02-19 18:59 UTC

This package is not auto-updated.

Last update: 2024-04-29 23:06:17 UTC


README

Install package :

composer require mar3q/http-client-php-example

How to use client by examples :

use Mar3q\HttpClientPhpExample\Client\Client;

$client = new Client();
$client->get('https://localhost', ['param' => 'value'])
$data = $response->getData();

How to customize client :

use Mar3q\HttpClientPhpExample\Client\Client;
use Mar3q\HttpClientPhpExample\Request\Config;

//create config
$config = new Config();
//add header
$config->addHeader(new Header('Content-Type', 'application/json'));
//add curl option
$config->addOption(new CurlOption(CURLOPT_TIMEOUT, 1));

//you can pass config to client instance, will be applied for every request
$client = new Client($config);
$client->get('https://localhost', ['param' => 'value'])
$data = $response->getData();

//or you can pass config to single request
$client = new Client();
$client->get('https://localhost', ['param' => 'value'], $config)
$data = $response->getData();

How to JWT auth example:

use Mar3q\HttpClientPhpExample\Client\Client;
use Mar3q\HttpClientPhpExample\Request\Config;

$client = new Client((new Config())->setBaseUri('https://localhost'));

$response = $client->post('/api/login', ['param' => 'value'], json_encode([
    'username' => 'username',
    'password' => 'password'
]));

$data = $response->getData();

$client->getConfig()->setJWTToken($data['token']);