PHP code example of stoyantodorov / laravel-api-client
1. Go to this page and download the library: Download stoyantodorov/laravel-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/ */
stoyantodorov / laravel-api-client example snippets
use Stoyantodorov\ApiClient\Interfaces\HttpClientInterface;
$apiClient = resolve(HttpClientInterface::class);
use Illuminate\Support\Facades\Http;
$apiClient->baseConfig(retries: 3, pendingRequest: Http::withToken($token));
use Stoyantodorov\ApiClient\Enums\HttpMethod;
use Stoyantodorov\ApiClient\Enums\HttpRequestFormat;
$apiClient->send(HttpMethod::CONNECT, 'https://exmple-host', HttpRequestFormat::QUERY, []);
use Illuminate\Support\Facades\Http;
use Stoyantodorov\ApiClient\Enums\HttpMethod;
use Stoyantodorov\ApiClient\Enums\HttpRequestFormat;
$apiClient->send(HttpMethod::CONNECT, 'https://exmple-host', HttpRequestFormat::QUERY, [], Http::withToken($token));
use Stoyantodorov\ApiClient\Enums\HttpClientRequestMethod;
$apiClient->->sendRequest(HttpClientRequestMethod::GET, 'https://exmple-host');
/**
* Base configuration for PendingRequest
* When PendingRequest instance isn't received, new one is created
*
* @param array $headers
* @param int $retries
* @param int $retryInterval
* @param int $timeout
* @param int $connectTimeout
* @param string|null $userAgent
* @param PendingRequest|null $pendingRequest
* @return self
*/
public function baseConfig(
array $headers = [],
int $retries = 1,
int $retryInterval = 1000,
int $timeout = 30,
int $connectTimeout = 3,
string|null $userAgent = null,
PendingRequest|null $pendingRequest = null,
): self;
/**
* Send a request with given HTTP method, url, options HTTP request format
* When PendingRequest instance isn't received, new one is created
*
* @param HttpMethod $httpMethod
* @param string $url
* @param HttpRequestFormat $format
* @param array $options
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function send(
HttpMethod $httpMethod,
string $url,
HttpRequestFormat $format,
array $options = [],
PendingRequest|null $pendingRequest = null
): Response|null;
/**
* Send HEAD request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $parameters
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function head(string $url, array $parameters = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send GET request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $parameters
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function get(string $url, array $parameters = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send POST request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $body
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function post(string $url, array $body = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send PUT request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $body
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function put(string $url, array $body = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send PATCH request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $body
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function patch(string $url, array $body = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send DELETE request
* When PendingRequest instance isn't received, new one is created
*
* @param string $url
* @param array $body
* @param PendingRequest|null $pendingRequest
* @return Response|null
*/
public function delete(string $url, array $body = [], PendingRequest|null $pendingRequest = null): Response|null;
/**
* Send a request by given HttpClient request method, url, options
* Catches RequestException and ConnectionException
* Logs messages
* Fires events depending on the configurations
* $httpMethod parameter should be provided when $apiClientRequestMethod is HttpClientRequestMethod::SEND
* When PendingRequest instance isn't received, new one is created
*
* @param HttpClientRequestMethod $apiClientRequestMethod
* @param string $url
* @param array $options
* @param PendingRequest|null $pendingRequest
* @param HttpMethod|null $httpMethod
* @return Response|null
*/
public function sendRequest(
HttpClientRequestMethod $apiClientRequestMethod,
string $url,
array $options = [],
PendingRequest|null $pendingRequest = null,
HttpMethod|null $httpMethod = null,
): Response|null;
/**
* Send request without error handling and event triggering
* Throw RequestException when throw is true
*
* @param HttpClientRequestMethod $apiClientMethod
* @param string $url
* @param array $options
* @param HttpMethod|null $httpMethod
* @param bool $throw
* @return Response
*/
public function request(
HttpClientRequestMethod $apiClientMethod,
string $url,
array $options = [],
HttpMethod|null $httpMethod = null,
bool $throw = false,
): Response;
/**
* Set PendingRequest when not null value is received
*
* @param PendingRequest|null $pendingRequest
* @return self
*/
public function setPendingRequest(PendingRequest|null $pendingRequest): self;
/**
* Get pendingRequest
*
* @return PendingRequest|null
*/
public function getPendingRequest(): PendingRequest|null;
/**
* Determine if HttpResponseSucceeded event is fired
*
* @param bool $value
* @return self
*/
public function fireEventOnSuccess(bool $value): self;
/**
* Determine if HttpRequestFailed event is fired
*
* @param bool $value
* @return self
*/
public function fireEventOnRequestException(bool $value): self;
/**
* Determine if HttpConnectionFailed event is fired
*
* @param bool $value
* @return self
*/
public function fireEventOnConnectionException(bool $value): self;
/**
* Determine if RequestException occurring is logged
*
* @param bool $value
* @return self
*/
public function logOnRequestException(bool $value): self;
/**
* Determine if ConnectionException occurring is logged
*
* @param bool $value
* @return self
*/
public function logOnConnectionException(bool $value): self;
enum HttpMethod: string
{
case HEAD = 'HEAD';
case GET = 'GET';
case POST = 'POST';
case PUT = 'PUT';
case PATCH = 'PATCH';
case DELETE = 'DELETE';
case CONNECT = 'CONNECT';
case OPTIONS = 'OPTIONS';
case TRACE = 'TRACE';
}
enum HttpRequestFormat: string
{
case QUERY = 'query';
case BODY = 'body';
case JSON = 'json';
case FORM_PARAMS = 'form_params';
case MULTIPART = 'multipart';
}
enum HttpClientRequestMethod: string
{
case GET = 'get';
case HEAD = 'head';
case POST = 'post';
case PATCH = 'patch';
case PUT = 'put';
case DELETE = 'delete';
case SEND = 'send';
}
/**
* Instantiate TokenInterface
* When receives $token it is set in TokenInterface instance
* $hasRefreshTokenRequest determines instantiating RefreshTokenData
* $configKey refers to token configurations in the config file
*
* @param bool $hasRefreshTokenRequest
* @param string $configKey
* @param string|null $token = null
* @return TokenInterface
*/
public static function create(
bool $hasRefreshTokenRequest = true,
string $configKey = 'tokenConfigurationsBase',
#[SensitiveParameter] string|null $token = null,
): TokenInterface;
/**
* Instantiate TokenInterface
* When receives $token it is set in TokenInterface instance
*
* @param TokenData $tokenData
* @param RefreshTokenData|null $refreshTokenData = null
* @param int $retries = 3
* @param string|null $token = null
* @return TokenInterface
*/
public static function create(
#[SensitiveParameter] TokenData $tokenData,
#[SensitiveParameter] RefreshTokenData|null $refreshTokenData = null,
int $retries = 3,
string|null $token = null,
): TokenInterface;
/**
* Get Access Token
* When it's missing request it
* When $refresh is true make a request to refresh the token
*
* @param bool $refresh = false
* @return string
*/
public function get(bool $refresh = false): string;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.