1. Go to this page and download the library: Download deeep/service-client-bundle 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/ */
use Deeep\ServiceClient\Contract\AuthRequiredInterface;
use Deeep\ServiceClient\Contract\MappableRequestInterface;
use Deeep\ServiceClient\Contract\ResponseInterface;
use Deeep\ServiceClient\Enum\HttpMethod;
use Deeep\ServiceClient\Request\AbstractRequest;
/**
* @implements MappableRequestInterface<User>
*/
final readonly class GetUserRequest extends AbstractRequest implements
AuthRequiredInterface,
MappableRequestInterface
{
public function __construct(
private int $userId,
) {}
public function getService(): string
{
return 'my_api';
}
public function getMethod(): HttpMethod
{
return HttpMethod::GET;
}
public function getUri(): string
{
return "/users/{$this->userId}";
}
/**
* @param ResponseInterface<mixed> $response
*/
public function mapResponse(ResponseInterface $response): User
{
$data = $response->toArray();
return new User($data['id'], $data['name'], $data['email']);
}
}
// Usage
$request = new GetUserRequest(123);
$response = $httpClient->send($request);
$user = $response->getData(); // User
use Deeep\ServiceClient\Contract\AuthRequiredInterface;
use Deeep\ServiceClient\Contract\MappableRequestInterface;
use Deeep\ServiceClient\Contract\ResponseInterface;
use Deeep\ServiceClient\Enum\HttpMethod;
use Deeep\ServiceClient\Request\AbstractRequest;
/**
* @implements MappableRequestInterface<User>
*/
final readonly class CreateUserRequest extends AbstractRequest implements
AuthRequiredInterface,
MappableRequestInterface
{
public function __construct(
private string $name,
private string $email,
) {}
public function getService(): string
{
return 'my_api';
}
public function getMethod(): HttpMethod
{
return HttpMethod::POST;
}
public function getUri(): string
{
return '/users';
}
/**
* @return array{name: string, email: string}
*/
public function getBody(): array
{
return [
'name' => $this->name,
'email' => $this->email,
];
}
/**
* @param ResponseInterface<mixed> $response
*/
public function mapResponse(ResponseInterface $response): User
{
$data = $response->toArray();
return new User($data['id'], $data['name'], $data['email']);
}
}