# HTTP
[![CI status](https://github.com/reactphp/http/workflows/CI/badge.svg)](https://github.com/reactphp/http/actions)
Event-driven, streaming HTTP client and server implementation for [ReactPHP](https://reactphp.org/).
This HTTP library provides re-usable implementations for an HTTP client and
server based on ReactPHP's [`Socket`](https://github.com/reactphp/socket) and
[`EventLoop`](https://github.com/reactphp/event-loop) components.
Its client component allows you to send any number of async HTTP/HTTPS requests
concurrently.
Its server component allows you to build plaintext HTTP and secure HTTPS servers
that accept incoming HTTP requests from HTTP clients (such as web browsers).
This library provides async, streaming means for all of this, so you can handle
multiple concurrent HTTP requests without blocking.
**Table of contents**
* [Quickstart example](#quickstart-example)
* [Client Usage](#client-usage)
* [Request methods](#request-methods)
* [Promises](#promises)
* [Cancellation](#cancellation)
* [Timeouts](#timeouts)
* [Authentication](#authentication)
* [Redirects](#redirects)
* [Blocking](#blocking)
* [Concurrency](#concurrency)
* [Streaming response](#streaming-response)
* [Streaming request](#streaming-request)
* [HTTP proxy](#http-proxy)
* [SOCKS proxy](#socks-proxy)
* [SSH proxy](#ssh-proxy)
* [Unix domain sockets](#unix-domain-sockets)
* [Server Usage](#server-usage)
* [HttpServer](#httpserver)
* [listen()](#listen)
* [Server Request](#server-request)
* [Request parameters](#request-parameters)
* [Query parameters](#query-parameters)
* [Request body](#request-body)
* [Streaming incoming request](#streaming-incoming-request)
* [Request method](#request-method)
* [Cookie parameters](#cookie-parameters)
* [Invalid request](#invalid-request)
* [Server Response](#server-response)
* [Deferred response](#deferred-response)
* [Streaming outgoing response](#streaming-outgoing-response)
* [Response length](#response-length)
* [Invalid response](#invalid-response)
* [Default response headers](#default-response-headers)
* [Middleware](#middleware)
* [Custom middleware](#custom-middleware)
* [Third-Party Middleware](#third-party-middleware)
* [API](#api)
* [Browser](#browser)
* [get()](#get)
* [post()](#post)
* [head()](#head)
* [patch()](#patch)
* [put()](#put)
* [delete()](#delete)
* [request()](#request)
* [requestStreaming()](#requeststreaming)
* [withTimeout()](#withtimeout)
* [withFollowRedirects()](#withfollowredirects)
* [withRejectErrorResponse()](#withrejecterrorresponse)
* [withBase()](#withbase)
* [withProtocolVersion()](#withprotocolversion)
* [withResponseBuffer()](#withresponsebuffer)
* [React\Http\Message](#reacthttpmessage)
* [Response](#response)
* [html()](#html)
* [json()](#json)
* [plaintext()](#plaintext)
* [xml()](#xml)
* [ServerRequest](#serverrequest)
* [ResponseException](#responseexception)
* [React\Http\Middleware](#reacthttpmiddleware)
* [StreamingRequestMiddleware](#streamingrequestmiddleware)
* [LimitConcurrentRequestsMiddleware](#limitconcurrentrequestsmiddleware)
* [RequestBodyBufferMiddleware](#requestbodybuffermiddleware)
* [RequestBodyParserMiddleware](#requestbodyparsermiddleware)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
## Quickstart example
Once [installed](#install), you can use the following code to access an
HTTP web server and send some simple HTTP GET requests:
```php
<?php
require __DIR__ . '/vendor/autoload.php';
$client = new React\Http\Browser();
$client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```
This is an HTTP server which responds with `Hello World!` to every request.
```php
<?php
require __DIR__ . '/vendor/autoload.php';
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return React\Http\Message\Response::plaintext(
"Hello World!\n"
);
});
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$http->listen($socket);
```
See also the [examples](examples/).
## Client Usage
### Request methods
Most importantly, this project provides a [`Browser`](#browser) object that
offers several methods that resemble the HTTP protocol methods:
```php
$browser->get($url, array $headers = array());
$browser->head($url, array $headers = array());
$browser->post($url, array $headers = array(), string|ReadableStreamInterface $body = '');
$browser->delete($url, array $headers = array(), string|ReadableStreamInterface $body = '');
$browser->put($url, array $headers = array(), string|ReadableStreamInterface $body = '');
$browser->patch($url, array $headers = array(), string|ReadableStreamInterface $body = '');
```
Each of these methods requires a `$url` and some optional parameters to send an
HTTP request. Each of these method names matches the respective HTTP request
method, for example the [`get()`](#get) method sends an HTTP `GET` request.
You can optionally pass an associative array of additional `$headers` that will be
sent with this HTTP request. Additionally, each method will automatically add a
matching `Content-Length` request header if an outgoing request body is given and its
size is known and non-empty. For an empty request body, if will only include a
`Content-Length: 0` request header if the request method usually expects a request
body (only applies to `POST`, `PUT` and `PATCH` HTTP request methods).
If you're using a [streaming request body](#streaming-request), it will default
to using `Transfer-Encoding: chunked` unless you explicitly pass in a matching `Content-Length`
request header. See also [streaming request](#streaming-request) for more details.
By default, all of the above methods default to sending requests using the
HTTP/1.1 protocol version. If you want to explicitly use the legacy HTTP/1.0
protocol version, you can use the [`withProtocolVersion()`](#withprotocolversion)
method. If you want to use any other or even custom HTTP request method, you can
use the [`request()`](#request) method.
Each of the above methods supports async operation and either *fulfills* with a
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface)
or *rejects* with an `Exception`.
Please see the following chapter about [promises](#promises) for more details.
### Promises
Sending requests is async (non-blocking), so you can actually send multiple
requests in parallel.
The `Browser` will respond to each request with a
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface)
message, the order is not guaranteed.
Sending requests uses a [Promise](https://github.com/reactphp/promise)-based
interface that makes it easy to react to when an HTTP request is completed
(i.e. either successfully fulfilled or rejected with an error):
```php
$browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) {
var_dump('Response received', $response);
},
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
```
If this looks strange to you, you can also use the more traditional [blocking API](#blocking).
Keep in mind that resolving the Promise with the full response message means the
whole response body has to be kept in memory.
This is easy to get started and works reasonably well for smaller responses
(such as common HTML pages or RESTful or JSON AP
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统 Vue File Manager Pro ,PHP多人协同网盘系统,外链分享、共享、聊天、OA协同系统
资源详情
资源评论
资源推荐
收起资源包目录
Vue File Manager Pro - v2.2.0.2.zip (2000个子文件)
artisan 2KB
phpstan.phar.asc 833B
random_compat.phar.pubkey.asc 488B
favicon.png.base64 1KB
completion.bash 3KB
carbon.bat 93B
run_tests.bat 72B
paratest.bat 70B
signing.c 13KB
crt.c 10KB
credentials.c 5KB
stream.c 5KB
logging.c 2KB
event_loop.c 1KB
http.c 970B
crc.c 872B
php_util.c 628B
awscrt.c 440B
.php_cs.cache 11KB
carbon 3KB
carbon 391B
.clang-format 2KB
.clang-format-ignore 61B
compile 1KB
composer 3KB
composer 3KB
web.config 1KB
COPYING 34KB
COPYING LESSER 7KB
ca-certificates.crt 219KB
bootstrap.min.css 158KB
tailwind.css 45KB
ignition.css 40KB
exception.css 14KB
whoops.base.css 10KB
nv.d3.min.css 9KB
app.css 9KB
default.css 5KB
default.css 4KB
prism.css 4KB
htmlDescriptor.css 3KB
exception_full.css 3KB
style.css 2KB
tailwind.css 1KB
error.css 275B
octicons.css 88B
custom.css 0B
.php_cs.dist 11KB
dashboard.html.dist 7KB
dashboard_branch.html.dist 7KB
file_branch.html.dist 2KB
file.html.dist 2KB
directory_branch.html.dist 2KB
directory.html.dist 2KB
file_item_branch.html.dist 1KB
directory_item_branch.html.dist 1KB
method_item_branch.html.dist 1KB
.php_cs.dist 1KB
phpunit.xml.dist 1KB
.php_cs.dist 1KB
.php_cs.dist 990B
phpunit.xml.dist 903B
file_item.html.dist 884B
phpunit.xml.dist 869B
phpunit.xml.dist 852B
directory_item.html.dist 833B
phpunit.xml.dist 703B
method_item.html.dist 685B
phpunit.xml.dist 676B
phpunit.xml.dist 639B
phpunit.xml.dist 619B
phpunit.xml.dist 563B
branches.html.dist 500B
paths.html.dist 498B
psalm.xml.dist 482B
phpunit.xml.dist 464B
phpunit.xml.dist 439B
phpunit.xml.dist 411B
phpunit.xml.dist 372B
.php_cs.dist 342B
phpstan.neon.dist 308B
coverage_bar.html.dist 295B
coverage_bar_branch.html.dist 295B
phpstan.neon.dist 213B
line.html.dist 197B
phpstan.neon.dist 194B
phpstan.neon.dist 161B
infection.json.dist 154B
lines.html.dist 101B
phpstan.neon.dist 66B
Dockerfile 202B
.dockerignore 5B
doctrine-dbal 3KB
doctrine-dbal 66B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
CSDN_SYSU
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0