1. Go to this page and download the library: Download rasuvaeff/context-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/ */
rasuvaeff / context-http example snippets
use Rasuvaeff\Context\Context;
use Rasuvaeff\Context\ContextController;
use Rasuvaeff\Context\Http\ContextMiddleware;
use Rasuvaeff\Context\Http\CorrelationId;
$middleware = new ContextMiddleware(
timeout: 5.0, // seconds, or fn (ServerRequestInterface): float
deadlineHeader: 'X-Request-Timeout-Ms', // optional: accept an upstream budget
responseFactory: $psr17Factory, // optional: map context exceptions to 504/499
);
// in the handler
$context = $request->getAttribute(Context::class);
$controller = $request->getAttribute(ContextController::class);
$correlationId = $request->getAttribute(CorrelationId::class); // always set
use Rasuvaeff\Context\Http\PreflightClient;
$client = new PreflightClient(
$psr18Client,
$context,
deadlineHeader: 'X-Request-Timeout-Ms', // optional: send the remaining budget
correlationHeader: 'X-Correlation-ID', // optional: forward the correlation id
);
$response = $client->sendRequest($request);
use Rasuvaeff\Context\Http\ContextClient;
[$context, $controller] = Context::background()->withTimeout(2.0);
$response = (new ContextClient($guzzle, deadlineHeader: 'X-Request-Timeout-Ms'))->send($request, $context);
$controller->cancel();
use Rasuvaeff\Context\WireCodec;
$codec = new WireCodec(['tenant' => TenantId::class, 'locale' => Locale::class]);
// incoming: baggage: tenant=acme,locale=de → $context->value(TenantId::class)
$middleware = new ContextMiddleware(timeout: 5.0, deadlineHeader: 'X-Request-Timeout-Ms', wireCodec: $codec);
// outgoing to YOUR services only — see Security
$internal = new ContextClient($guzzle, deadlineHeader: 'X-Request-Timeout-Ms', wireCodec: $codec);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.