1. Go to this page and download the library: Download programmatordev/php-api-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
useProgrammatorDev\Api\Api;
classYourApiextendsApi{
publicfunction__construct(string $language = 'en'){
// ...$this->addHeaderDefault('X-LANGUAGE', $language);
}
publicfunctiongetPosts(): string{
// GET https://api.example.com/v1/posts with an 'X-LANGUAGE' => 'en' header valuereturn$this->request(
method: 'GET',
path: '/posts'
);
}
publicfunctiongetCategories(): string{
// a header with the same name, passed in the request method, will overwrite a header default// GET https://api.example.com/v1/categories with an 'X-LANGUAGE' => 'pt' header valuereturn$this->request(
method: 'GET',
path: '/categories',
headers: [
'X-LANGUAGE' => 'pt'
]
);
}
}
$this->addPreRequestListener(callable $listener, int $priority = 0): self;
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Event\PreRequestEvent;
classYourApiextendsApi{
publicfunction__construct(){
// a PreRequestEvent is passed as an argument$this->addPreRequestListener(function(PreRequestEvent $event){
$request = $event->getRequest();
if ($request->getMethod() === 'POST') {
// do something for all POST requests// ...
}
});
}
// ...
}
$this->addPreRequestListener(function(PreRequestEvent $event){
// get request data
$request = $event->getRequest();
// ...// set request data
$event->setRequest($request);
});
$this->addPostRequestListener(callable $listener, int $priority = 0): self;
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Event\PostRequestEvent;
classYourApiextendsApi{
publicfunction__construct(){
// a PostRequestEvent is passed as an argument$this->addPostRequestListener(function(PostRequestEvent $event){
$response = $event->getResponse();
$statusCode = $response->getStatusCode();
// if there was a response with an error status codeif ($statusCode >= 400) {
// throw an exception
match ($statusCode) {
400 => thrownew BadRequestException(),
404 => thrownew NotFoundException(),
default => thrownew UnexpectedErrorException()
};
}
});
}
// ...
}
$this->addPostRequestListener(function(PostRequestEvent $event){
// get request data
$request = $event->getRequest();
// get response data
$response = $event->getResponse();
// ...// set response data
$event->setResponse($response);
});
$this->addResponseContentsListener(callable $listener, int $priority = 0): self;
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Event\ResponseContentsEvent;
classYourApiextendsApi{
publicfunction__construct(){
// a ResponseContentsEvent is passed as an argument$this->addResponseContentsListener(function(ResponseContentsEvent $event){
// get response contents and decode json string into an array
$contents = $event->getContents();
$contents = json_decode($contents, true);
// set handled contents
$event->setContents($contents);
});
}
publicfunctiongetPosts(): array{
// will return an arrayreturn$this->request(
method: 'GET',
path: '/posts'
);
}
}
$this->addResponseContentsListener(function(ResponseContentsEvent $event){
// get response body contents data
$contents = $event->getContents();
// ...// set contents
$event->setContents($contents);
});
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Event\ResponseContentsEvent;
classYourApiextendsApi{
publicfunction__construct(){
// two event listeners are added,// but the second is executed first (higher priority) even though it was added after// executed last (lower priority)$this->addResponseContentsListener(
listener: function(ResponseContentsEvent $event){ ... },
priority: 0
);
// executed first (higher priority)$this->addResponseContentsListener(
listener: function(ResponseContentsEvent $event){ ... },
priority: 10
);
}
}
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Event\ResponseContentsEvent;
classYourApiextendsApi{
publicfunction__construct(){
$this->addResponseContentsListener(function(ResponseContentsEvent $event){
// stop propagation so future listeners of this event will not be called
$event->stopPropagation();
});
// this listener will not be called$this->addResponseContentsListener(function(ResponseContentsEvent $event){
// ...
});
}
}
useProgrammatorDev\Api\Builder\ClientBuilder;
new ClientBuilder(
// a PSR-18 client
?ClientInterface $client = null,
// a PSR-17 request factory
?RequestFactoryInterface $requestFactory = null,
// a PSR-17 stream factory
?StreamFactoryInterface $streamFactory = null
);
useHttp\Client\Common\Plugin;
$this->getClientBuilder()->addPlugin(Plugin $plugin, int $priority): self;
useProgrammatorDev\Api\Api;
useHttp\Client\Common\Plugin\RetryPlugin;
classYourApiextendsApi{
publicfunction__construct(){
// ...// if a request fails, it will retry at least 3 times// priority is 20 to execute before the cache plugin// (check the above plugin order list for more information)$this->getClientBuilder()->addPlugin(
plugin: new RetryPlugin(['retries' => 3]),
priority: 20
);
}
}
useProgrammatorDev\Api\Builder\CacheBuilder;
usePsr\Cache\CacheItemPoolInterface;
new CacheBuilder(
// a PSR-6 cache adapter
CacheItemPoolInterface $pool,
// default lifetime (in seconds) of cache items
?int $ttl = 60,
// An array of HTTP methods for which caching should be applied
$methods = ['GET', 'HEAD'],
// An array of cache directives to be compared with the headers of the HTTP response,// in order to determine cacheability
$responseCacheDirectives = ['max-age']
);
useProgrammatorDev\Api\Api;
useProgrammatorDev\Api\Builder\CacheBuilder;
useSymfony\Component\Cache\Adapter\FilesystemAdapter;
classYourApiextendsApi{
publicfunction__construct(){
// ...
$pool = new FilesystemAdapter();
// file-based cache adapter with a 1-hour default cache lifetime$this->setCacheBuilder(
new CacheBuilder(
pool: $pool,
ttl: 3600
)
);
}
publicfunctiongetPosts(): string{
// you can change the lifetime (and all other parameters)// for this specific endpoint$this->getCacheBuilder()->setTtl(600);
return$this->request(
method: 'GET',
path: '/posts'
);
}
}
useProgrammatorDev\Api\Builder\LoggerBuilder;
usePsr\Log\LoggerInterface;
useHttp\Message\Formatter;
useHttp\Message\Formatter\SimpleFormatter;
new LoggerBuilder(
// a PSR-3 logger adapter
LoggerInterface $logger,
// determines how the log entries will be formatted when they are written by the logger// if no formatter is provided, it will default to a SimpleFormatter instance
?Formatter $formatter = null
);