1. Go to this page and download the library: Download sokil/php-rest-api-client 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/ */
sokil / php-rest-api-client example snippets
use Sokil\RestApiClient\RestApiClient;
$restApiClient = new RestApiClient(
$psrHttpClient,
$psrRequestFactory,
$psrUriFactory,
null,
null,
);
use Psr\Http\Message\ResponseInterface;
use Sokil\RestApiClient\Request\AbstractApiRequest
class GetResourceByIdRequest extends AbstractApiRequest
{
public function __construct(
private readonly string $id
) {
parent::__construct(
'GET',
sprintf('/v1/resources/%s', $this->id),
);
}
public function parseResponse(ResponseInterface $response): ?object
{
if ($response->getStatusCode() !== 200) {
throw new \Exception('Response code not ok');
}
return new SomeResourceDTO();
}
}
class MyService
{
public function getResource(): object
{
$id = 'id_string';
$result = $this->restApiClient->call(new GetResourceByIdRequest($id))
return $result;
}
}
use Sokil\RestApiClient\RestApiClient;
use Sokil\RestApiClient\Auth\OAuth2\OAuth2Client;
use Sokil\RestApiClient\Auth\OAuth2\AuthTokenStorage\SimpleCacheAuthTokenStorage;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Psr\Log\NullLogger;
$symfonyClient = HttpClient::createForBaseUri(getenv('API_HOST'));
$psrHttpClient = new Psr18Client($symfonyClient);
// used to authorize on authorization server, store and refresh stored access token
$oAuth2Client = new OAuth2Client(
getenv('AUTH_API_HOST'),
getenv('AUTH_CLIENT_ID'),
getenv('AUTH_CLIENT_SECRET'),
$psrHttpClient,
$psrHttpClient,
new SimpleCacheAuthTokenStorage(new Psr16Cache(new ArrayAdapter())),
new NullLogger(),
);
$restApiClient = new RestApiClient(
$psrHttpClient,
$psrHttpClient,
$psrHttpClient,
$psrHttpClient,
$oAuth2Client,
);
use Psr\Http\Message\ResponseInterface;
use Sokil\RestApiClient\Request\AbstractApiRequest
use Sokil\RestApiClient\Auth\OAuth2\OAuth2AuthorizationAwareInterface
class GetResourceByIdRequest extends AbstractApiRequest implements OAuth2AuthorizationAwareInterface
{
public function __construct(
private readonly string $id
) {
parent::__construct(
'GET',
sprintf('/v1/resource/%s', $this->id),
);
}
public function parseResponse(ResponseInterface $response): ?object
{
if ($response->getStatusCode() !== 200) {
throw new \Exception('Response code not ok');
}
$body = $this->jsonResponseToArray($response);
if (empty($body) || !isset($body['resource'])) {
throw new \Exception('Response body invalid');
}
return new YourResponseDto(..$values);
}
}
use Sokil\RestApiClient\RestApiClient;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Sokil\RestApiClient\HeaderSecretAuth\SimpleSecretProvider;
use Psr\Log\NullLogger;
$symfonyClient = HttpClient::createForBaseUri(getenv('API_HOST'));
$psrHttpClient = new Psr18Client($symfonyClient);
// used to authorize on authorization server, store and refresh stored access token
$oAuth2Client = ;
$restApiClient = new RestApiClient(
$psrHttpClient,
$psrHttpClient,
$psrHttpClient,
null,
new SimpleHeaderSecretProvider(
getenv('TOKAN_AUTH_HEADER_NAME'),
getenv('TOKAN_AUTH_SECRET'),
)
);
use Psr\Http\Message\ResponseInterface;
use Sokil\RestApiClient\Request\AbstractApiRequest
use Sokil\RestApiClient\Auth\OAuth2\OAuth2AuthorizationAwareInterface
class GetResourceByIdRequest extends AbstractApiRequest implements HeaderSecretAuthorizationAwareInterface
{
public function __construct(
private readonly string $id
) {
parent::__construct(
'GET',
sprintf('/v1/resource/%s', $this->id),
);
}
public function parseResponse(ResponseInterface $response): ?object
{
if ($response->getStatusCode() !== 200) {
throw new \Exception('Response code not ok');
}
$body = $this->jsonResponseToArray($response);
if (empty($body) || !isset($body['resource'])) {
throw new \Exception('Response body invalid');
}
return new YourResponseDto(..$values);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.