PHP code example of givanov95 / typed-http
1. Go to this page and download the library: Download givanov95/typed-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/ */
givanov95 / typed-http example snippets
bash
composer 95\TypedHttp\Requests\Request;
use Givanov95\TypedHttp\Requests\Authorization\BasicAuth\BasicAuthAuthenticator;
use Givanov95\TypedHttp\Requests\Authorization\AuthenticatorInterface;
class MyRequest extends Request implements BasicAuthInterface
{
// Required param for the request
public string $param1 = null;
// Optional param for the request
public ?int $param2 = null;
public function __construct(
?string $param1 = null,
?int $param2 = null
)
{
parent::__construct();
$this->param1 = $param1;
$this->param2 = $param2;
}
public function getAuthenticator(): BasicAuthAuthenticator
{
return new BasicAuthAuthenticator('username', 'password');
}
public function getUrl(): Url
{
return new Url('https://api.example.com/data');
}
protected function getHttpMethodType(): HttpMethod
{
return HttpMethod::POST;
}
protected function getContentType(): ContentType
{
return ContentType::JSON;
}
protected function getExpectedResponseFormat(): ExpectedResponseFormat
{
return ExpectedResponseFormat::JSON;
}
}
// Params are set via public properties declared in your extending Request class
$request = new MyRequest(param1: 'value1', param2: 42);
$response = $request->executeRequest();
$data = $request->getParsedBody();
print_r($data);