PHP code example of ijodkor / quick-http

1. Go to this page and download the library: Download ijodkor/quick-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/ */

    

ijodkor / quick-http example snippets


/**
 * Middleware to refresh token
 * @return Closure
 */
class EDMRequestService extends HttpBearerRequestService {

    public function __construct(readonly EDMAuthService $authService) {
        parent::__construct();

        $url = config('integration.edm_api_url');
        $this->setUrl("$url/document");
        $this->setCredentials($authService->getToken());
    }

    protected function middleware(): Closure {
        return function (callable $handler) {
            return function (RequestInterface $request, array $options) use ($handler) {
                $promise = $handler($request, $options);
                return $promise->then(function (ResponseInterface $response) use ($handler, $request, $options) {
                    /* @var Promise $promise */

                    if (in_array($response->getStatusCode(), [401, 403])) {
                        $token = $this->authService->login();
                        $request = $request->withHeader('Authorization', "Bearer $token");

                        // Retry request after refreshing token
                        // $promise->wait();
                        return $handler($request, $options);
                    }

                    return $response;
                });
            };
        };
    }
}