1. Go to this page and download the library: Download thingston/psr18 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/ */
thingston / psr18 example snippets
use Thingston\Psr17\RequestFactory;
use Thingston\Psr18\Client;
$request = (new RequestFactory())->createRequest('GET', 'https://api.example.com/data');
$response = (new Client())->sendRequest($request);
echo $response->getStatusCode(); // 200
echo (string) $response->getBody();
use Thingston\Psr18\AuthPolicy;
use Thingston\Psr18\CircuitBreakerPolicy;
use Thingston\Psr18\Client;
use Thingston\Psr18\CookieJar;
use Thingston\Psr18\Curl\RequestOptions;
use Thingston\Psr18\Curl\ResponseOptions;
use Thingston\Psr18\ProxyOptions;
use Thingston\Psr18\RedirectPolicy;
use Thingston\Psr18\RetryPolicy;
use Thingston\Psr18\TimeoutPolicy;
use Thingston\Psr18\TlsPolicy;
$client = new Client(
requestOptions: new RequestOptions([RequestOptions::USER_AGENT => 'my-app/1.0']),
responseOptions: new ResponseOptions([ResponseOptions::ENCODING => '']),
retryPolicy: new RetryPolicy(maxRetries: 3, delayMilliseconds: 200),
redirectPolicy: new RedirectPolicy(followRedirects: true, maxRedirects: 10),
tlsPolicy: new TlsPolicy(verifyPeer: true, verifyHost: true),
timeoutPolicy: new TimeoutPolicy(connectTimeoutMs: 5_000, transferTimeoutMs: 30_000),
authPolicy: AuthPolicy::bearer('my-token'),
proxyOptions: new ProxyOptions('http://proxy.internal:3128'),
cookieJar: new CookieJar(),
circuitBreaker: new CircuitBreakerPolicy(failureThreshold: 5, resetTimeoutSeconds: 60),
logger: $psr3Logger,
middleware: [$myMiddleware],
);
use Thingston\Psr18\Client;
use Thingston\Psr18\RedirectPolicy;
// Follow up to 10 redirects (default)
$client = new Client(redirectPolicy: new RedirectPolicy());
// Follow redirects while preserving the HTTP method on all redirect types
$client = new Client(redirectPolicy: new RedirectPolicy(followRedirects: true, preserveMethod: true));
// Disable redirect following — caller receives the 3xx response
$client = new Client(redirectPolicy: new RedirectPolicy(followRedirects: false));
use Thingston\Psr18\Client;
use Thingston\Psr18\RetryPolicy;
// Constant 200 ms delay, up to 3 retries
$client = new Client(
retryPolicy: new RetryPolicy(maxRetries: 3, delayMilliseconds: 200),
);
// Exponential backoff: 100 ms → 200 ms → 400 ms, capped at 2 000 ms
$client = new Client(
retryPolicy: new RetryPolicy(
maxRetries: 4,
delayMilliseconds: 100,
backoffMultiplier: 2.0,
maxDelayMilliseconds: 2_000,
),
);
// Retry only on GET and HEAD, and only on 503
$client = new Client(
retryPolicy: new RetryPolicy(
maxRetries: 3,
methods: ['GET', 'HEAD'],
statusCodes: [503],
),
);
use Thingston\Psr18\Client;
use Thingston\Psr18\TlsPolicy;
// Custom CA bundle and client certificate
$client = new Client(
tlsPolicy: new TlsPolicy(
verifyPeer: true,
verifyHost: true,
caInfo: '/etc/ssl/certs/ca-bundle.crt',
certificate: '/path/to/client.crt',
privateKey: '/path/to/client.key',
),
);
// Disable verification (testing only — never in production)
$client = new Client(
tlsPolicy: new TlsPolicy(verifyPeer: false, verifyHost: false),
);
use Thingston\Psr18\Client;
use Thingston\Psr18\TimeoutPolicy;
$client = new Client(
timeoutPolicy: new TimeoutPolicy(
connectTimeoutMs: 3_000, // fail fast if the host is unreachable
transferTimeoutMs: 30_000, // allow 30 s for the full response
),
);
use Thingston\Psr18\AuthPolicy;
use Thingston\Psr18\Client;
$client = new Client(authPolicy: AuthPolicy::basic('alice', 's3cr3t'));
$client = new Client(authPolicy: AuthPolicy::bearer('eyJhbGciOi...'));
$client = new Client(authPolicy: AuthPolicy::digest('alice', 's3cr3t'));
use Thingston\Psr18\Client;
use Thingston\Psr18\ProxyOptions;
$client = new Client(
proxyOptions: new ProxyOptions(
proxy: 'http://proxy.corp:3128',
proxyType: CURLPROXY_HTTP, // default; use CURLPROXY_SOCKS5 etc. as needed
userPassword: 'proxyuser:proxypass',
noProxy: 'localhost,127.0.0.1,.internal',
),
);
use Thingston\Psr17\RequestFactory;
use Thingston\Psr18\MultiClient;
$factory = new RequestFactory();
$client = new MultiClient();
$responses = $client->sendAll([
$factory->createRequest('GET', 'https://api.example.com/users'),
$factory->createRequest('GET', 'https://api.example.com/orders'),
$factory->createRequest('GET', 'https://api.example.com/products'),
]);
// Responses are indexed in the same order as the requests
echo $responses[0]->getStatusCode();
use Thingston\Psr18\Client;
use Thingston\Psr18\CookieJar;
// Temporary file, automatically deleted when the CookieJar goes out of scope
$jar = new CookieJar();
$client = new Client(cookieJar: $jar);
// Persistent file
$client = new Client(cookieJar: new CookieJar('/var/run/session-cookies.txt'));
use Thingston\Psr18\CircuitBreakerPolicy;
use Thingston\Psr18\Client;
$circuitBreaker = new CircuitBreakerPolicy(
failureThreshold: 5, // open after 5 consecutive failures
resetTimeoutSeconds: 30, // try again after 30 s
);
$client = new Client(circuitBreaker: $circuitBreaker);
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thingston\Psr18\Client;
use Thingston\Psr18\MiddlewareInterface;
final class RequestIdMiddleware implements MiddlewareInterface
{
public function process(RequestInterface $request, callable $next): ResponseInterface
{
return $next($request->withHeader('X-Request-Id', uniqid()));
}
}
$client = new Client(middleware: [new RequestIdMiddleware()]);
use Thingston\Psr18\Client;
$client = new Client(logger: $monologLogger);
use Thingston\Psr18\Client;
use Thingston\Psr18\Curl\RequestOptions;
use Thingston\Psr18\Curl\ResponseOptions;
$client = new Client(
requestOptions: (new RequestOptions())
->withOption(RequestOptions::USER_AGENT, 'my-app/1.0'),
responseOptions: (new ResponseOptions())
->withOption(ResponseOptions::ENCODING, ''),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.