A PHP implementation of JavaScript's
Request
,Response
,URL
,URLSearchParams
,FormData
, andHeaders
interfaces.
<?php
use \shgysk8zer0\HTTP\{Request, Response, Body, FormData, Headers, File, URL};
use \shgysk8zer0\HTTP\Abstracts\{HTTPStatusCodes as HTTP};
use \shgysk8zer0\PHPAPI\{ConsoleLogger, FileCache};
use \DateInterval;
spl_autoload_regiser('spl_autoload');
set_include_path($classes_dir . PATH_SEPARATOR . get_include_path());
try {
$url = new URL('../some/endpoint', 'https://example.com/wrong-path/');
$cache = new FileCache();
$logger = new ConsoleLogger();
$req = new Request($url, [
'method' => 'POST',
'referrer' => 'no-referrer',
'redirect' => 'follow',
'credentials' => 'omit',
'cache' => 'default',
'headers' => new Headers([
'Accept' => 'application/json',
'X-FOO' => 'bar',
]),
'body' => new FormData([
'username' => $username,
'token' => $token,
'file' => new File($filename, null, 'file'),
'upload' => new UploadFile('upload'),
])
]);
$req->setLogger($logger);
// For compatibility with `CacheInterface` `Request.cache = 'default'` -> `Request::setCacheMode('default')`
// and `Request::setCache(CacheInterface $cache)`
$req->setCache($cache);
if ($resp = $req->send($timeout)) {
$resp->headers->set('Content-SecurityPolicy', ContentSecurityPolicy::fromIniFile('./csp.ini'));
$resp->headers->set('Feature-Policy', new FeaturePolicy([
'geolocation' => 'self',
'camera' => 'self',
]));
$resp->headers->append('Set-Cookie', new Cookie('name','value', [
'secure' => true,
'httpOnly' => true,
'expires' => new DateInterval('P1D'),
'sameSite' => 'Strict',
]);
// `Response::send()` sends HTTP status code, headers, & body
$resp->send();
} else {
$resp = new Response(new Body('An unknown error occured'), [
'headers' => new Headers([
'Content-Security-Policy' => new ContentSecurityPolicy(['default-src' => 'self']),
'Content-Type' => 'text/plain',
]),
'status' => HTTP::BAD_GATEWAY,
]);
$resp->send();
}
} catch (Throwable $e) {
$logger->error('[{class} {code}] "{message}" at {file}:{line}', [
'class' => get_class($e),
'code' => $e->getCode(),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
$resp = new Response(new Body('An error occured'), [
'status' => HTTP::INTERNAL_SERVER_ERROR,
'headers' => new Headers([
'Content-Type' => 'text/plain',
]),
]);
$resp->send();
}
This is built to be installed as a submodule and loaded using spl_autoload
To install, just
git submodule add https://github.com/shgysk8zer0/http.git $classes_dir/shgysk8zer0/http
Loggers and caches are used from shgysk8zer0/PHPAPI.
You will need to add that as a submodule to $classes_dir/shgysk8zer0/phpapi
.