https://github.com/shgysk8zer0/http
PHP implementation of JavaScript's Request, Response, Headers, & URL classes
https://github.com/shgysk8zer0/http
fetch-api formdata headers php php7 request response url urlsearchparams
Last synced: about 1 year ago
JSON representation
PHP implementation of JavaScript's Request, Response, Headers, & URL classes
- Host: GitHub
- URL: https://github.com/shgysk8zer0/http
- Owner: shgysk8zer0
- License: mit
- Created: 2020-06-07T16:10:13.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-23T23:30:03.000Z (almost 6 years ago)
- Last Synced: 2025-02-02T01:44:42.417Z (about 1 year ago)
- Topics: fetch-api, formdata, headers, php, php7, request, response, url, urlsearchparams
- Language: PHP
- Size: 149 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# http
[](https://github.com/shgysk8zer0/http/actions?query=workflow%3A%22PHP+Lint%22)
[](https://github.com/shgysk8zer0/http/actions?query=workflow%3A%22Lint+Code+Base%22)
[](https://github.com/shgysk8zer0/http/blob/master/LICENSE)


[](https://liberapay.com/shgysk8zer0/donate "Donate using Liberapay")






- - -
> A PHP implementation of JavaScript's [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request),
[`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response),
[`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL/),
[`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams),
[`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData),
and [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
interfaces.
## Example
```php
'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();
}
```
## Installation
This is built to be installed as a submodule and loaded using [`spl_autoload`](https://www.php.net/manual/en/function.spl-autoload)
To install, just
```bash
git submodule add https://github.com/shgysk8zer0/http.git $classes_dir/shgysk8zer0/http
```
## Dependencies
Loggers and caches are used from [shgysk8zer0/PHPAPI](https://github.com/shgysk8zer0/phpapi).
You will need to add that as a submodule to `$classes_dir/shgysk8zer0/phpapi`.