1. Go to this page and download the library: Download flytachi/winter-cast 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/ */
flytachi / winter-cast example snippets
use Flytachi\Winter\Cast\Cast;
// One-liner
$response = Cast::sendGet('https://api.example.com/users');
$users = $response->json();
// Or with fluent API
$response = Cast::get('https://api.example.com/users')
->timeout(5)
->send();
use Flytachi\Winter\Cast\Exception\TimeoutException;
use Flytachi\Winter\Cast\Exception\ConnectionException;
use Flytachi\Winter\Cast\Exception\RequestException;
try {
$response = Cast::get($url)
->throwOnError() // Enable exceptions for HTTP errors
->send();
} catch (TimeoutException $e) {
// Request timed out - retry with longer timeout
echo "Timeout: {$e->getMessage()}";
} catch (ConnectionException $e) {
// Connection failed - try backup server
echo "Connection failed: {$e->getMessage()}";
} catch (RequestException $e) {
// HTTP error (4xx/5xx) - access response object
echo "HTTP {$e->response->statusCode}: {$e->response->body()}";
}
use Flytachi\Winter\Cast\Common\CastClient;
use Flytachi\Winter\Cast\Common\CastRequest;
class ApiService
{
public function __construct(
private readonly CastClient $httpClient
) {}
public function fetchUsers(): array
{
$request = CastRequest::get('https://api.com/users')
->timeout(5);
$response = $this->httpClient->send($request);
return $response->json();
}
}
// DI Container configuration
$client = new CastClient(
defaultTimeout: 30,
defaultConnectTimeout: 10
);
$service = new ApiService($client);
use Flytachi\Winter\Cast\Common\CastMiddleware;
use Flytachi\Winter\Cast\Common\CastRequest;
use Flytachi\Winter\Cast\Common\CastResponse;
class TimingMiddleware implements CastMiddleware
{
public function handle(CastRequest $request, callable $next): CastResponse
{
$start = microtime(true);
$response = $next($request); // Pass to next middleware/client
$duration = microtime(true) - $start;
echo "Request took {$duration}s\n";
return $response;
}
}
use Flytachi\Winter\Cast\Common\CastClient;
$client = new CastClient();
$client
->addMiddleware(new LoggingMiddleware($logger))
->addMiddleware(new BearerAuthMiddleware($token))
->addMiddleware(new TimingMiddleware());
// Middleware executes in order: Logging → Auth → Timing → Request
$client = new CastClient();
$client->addMiddleware(new BearerAuthMiddleware($token));
Cast::setGlobalClient($client);
// All requests now use middleware
Cast::sendGet('https://api.com/data');
use Flytachi\Winter\Cast\Stereotype\LoggingMiddleware;
$client->addMiddleware(new LoggingMiddleware(
logger: $psrLogger,
requestLevel: LogLevel::INFO, // Log level for requests
responseLevel: LogLevel::INFO, // Log level for 2xx/3xx
errorLevel: LogLevel::WARNING, // Log level for 4xx/5xx
logBody: false, // Log request/response body
bodyMaxLength: 500 // Truncate body at N chars
));
// Output:
// [INFO] HTTP Request: GET https://api.com/users
// [INFO] HTTP Response: 200 (125.4ms)
use Flytachi\Winter\Cast\Stereotype\BearerAuthMiddleware;
// Static token
$client->addMiddleware(new BearerAuthMiddleware('your-api-token'));
// Dynamic token (fetched for each request)
$client->addMiddleware(new BearerAuthMiddleware(
fn() => $tokenService->getAccessToken()
));
use Flytachi\Winter\Cast\Stereotype\BasicAuthMiddleware;
$client->addMiddleware(new BasicAuthMiddleware('username', 'password'));
use Flytachi\Winter\Cast\Stereotype\HeadersMiddleware;
$client->addMiddleware(new HeadersMiddleware(
CastHeader::instance()
->userAgent('MyApp/1.0')
->acceptLanguage('ru')
->set('X-API-Version', '2')
));
use Flytachi\Winter\Cast\Stereotype\RetryOnUnauthorizedMiddleware;
$client->addMiddleware(new RetryOnUnauthorizedMiddleware(
tokenRefresher: function (): string {
// Refresh your token
$response = Cast::sendPost('https://auth.api.com/refresh', [
'refresh_token' => $storedRefreshToken
]);
return $response->json()['access_token'];
},
maxRetries: 1 // Retry once after refresh
));
use Flytachi\Winter\Cast\Stereotype\ApiService;
use Flytachi\Winter\Cast\Common\CastHeader;
class PaymentApi extends ApiService
{
protected static function baseUrl(): string
{
return 'https://api.payment.com/v1';
}
protected static function headers(): CastHeader
{
return CastHeader::instance()
->authBearer(env('PAYMENT_TOKEN'))
->json();
}
public static function getBalance(): array
{
$response = self::get('balance')->send(self::client());
return self::tryResult($response);
}
public static function createPayment(array $data): array
{
$response = self::post('payments')
->withJsonBody($data)
->send(self::client());
return self::tryResult($response);
}
}
// Usage
$balance = PaymentApi::getBalance();
$payment = PaymentApi::createPayment(['amount' => 100]);
class PaymentApi extends ApiService
{
protected static function baseUrl(): string
{
return env('PAYMENT_API_URL');
}
protected static function headers(): CastHeader
{
return CastHeader::instance()->json();
}
protected static function createClient(): CastClient
{
return (new CastClient(defaultTimeout: 30))
->addMiddleware(new LoggingMiddleware($logger))
->addMiddleware(new BearerAuthMiddleware(
fn() => TokenService::getToken()
))
->addMiddleware(new RetryOnUnauthorizedMiddleware(
fn() => TokenService::refresh()
));
}
}