[已过时] 简单的异步 PSR-7 HTTP 客户端,可同时处理任意数量的 HTTP 请求,基于 ReactPHP 构建。
[已过时] 简单的异步 PSR-7 HTTP 客户端,可同时处理任意数量的 HTTP 请求,基于 ReactPHP 构建。
This package has now been migrated over to react/http and only exists for BC reasons.
$ composer require react/httpIf you've previously used this package, upgrading should take no longer than a few minutes.
All classes have been merged as-is from the latest v2.9.0 release with no other significant changes,
so you can simply update your code to use the updated namespace like this:
// old
$browser = new Clue\React\Buzz\Browser($loop);
$browser->get($url);
// new
$browser = new React\Http\Browser($loop);
$browser->get($url);See react/http for more details.
The below documentation applies to the last release of this package. Further development will take place in the updated react/http, so you're highly recommended to upgrade as soon as possible.
Simple, async PSR-7 HTTP client for concurrently processing any number of HTTP requests, built on top of ReactPHP.
This library is heavily inspired by the great kriswallsmith/Buzz project. However, instead of blocking on each request, it relies on ReactPHP's EventLoop to process multiple requests in parallel. This allows you to interact with multiple HTTP servers (fetch URLs, talk to RESTful APIs, follow redirects etc.) at the same time. Unlike the underlying react/http-client, this project aims at providing a higher-level API that is easy to use in order to process multiple HTTP requests concurrently without having to mess with most of the low-level details.
Table of contents
We invest a lot of time developing, maintaining and updating our awesome open-source projects. You can help us sustain this high-quality of our work by becoming a sponsor on GitHub. Sponsors get numerous benefits in return, see our sponsoring page for details.
Let's take these projects to the next level together!
Once installed, you can use the following code to access a HTTP webserver and send some simple HTTP GET requests:
$loop = React\EventLoop\Factory::create();
$client = new Clue\React\Buzz\Browser($loop);
$client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
});
$loop->run();See also the examples.
Most importantly, this project provides a Browser object that
offers several methods that resemble the HTTP protocol methods:
$browser->get($url, array $headers = array());
$browser->head($url, array $headers = array());
$browser->post($url, array $headers = array(), string|ReadableStreamInterface $contents = '');
$browser->delete($url, array $headers = array(), string|ReadableStreamInterface $contents = '');
$browser->put($url, array $headers = array(), string|ReadableStreamInterface $contents = '');
$browser->patch($url, array $headers = array(), string|ReadableStreamInterface $contents = '');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() 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, it will default
to using Transfer-Encoding: chunked unless you explicitly pass in a matching Content-Length
request header. See also 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()
method. If you want to use any other or even custom HTTP request method, you can
use the request() method.
Each of the above methods supports async operation and either fulfills with a
ResponseInterface or rejects with an Exception.
Please see the following chapter about promises for more details.
Sending requests is async (non-blocking), so you can actually send multiple
requests in parallel.
The Browser will respond to each request with a ResponseInterface
message, the order is not guaranteed.
Sending requests uses a 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):
$browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) {
var_dump('Response received', $response);
},
function (Exception $error) {
var_dump('There was an error', $error->getMessage());
}
);If this looks strange to you, you can also use the more traditional blocking API.
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 API requests).
You may also want to look into the streaming API:
The returned Promise is implemented in such a way that it can be cancelled when it is still pending. Cancelling a pending promise will reject its value with an Exception and clean up any underlying resources.
$promise = $browser->get($url);
$loop->addTimer(2.0, function () use ($promise) {
$promise->cancel();
});This library uses a very efficient HTTP implementation, so most HTTP requests
should usually be completed in mere milliseconds. However, when sending HTTP
requests over an unreliable network (the internet), there are a number of things
that can go wrong and may cause the request to fail after a time. As such, this
library respects PHP's default_socket_timeout setting (default 60s) as a timeout
for sending the outgoing HTTP request and waiting for a successful response and
will otherwise cancel the pending request and reject its value with an Exception.
Note that this timeout value covers creating the underlying transport connection, sending the HTTP request, receiving the HTTP response headers and its full response body and following any eventual redirects. See also redirects below to configure the number of redirects to follow (or disable following redirects altogether) and also streaming below to not take receiving large response bodies into account for this timeout.
You can use the withTimeout() method to pass a custom timeout
value in seconds like this:
$browser = $browser->withTimeout(10.0);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// response received within 10 seconds maximum
var_dump($response->getHeaders());
});Similarly, you can use a bool false to not apply a timeout at all
or use a bool true value to restore the default handling.
See withTimeout() for more details.
If you're using a streaming response body, the time it takes to receive the response body stream will not be included in the timeout. This allows you to keep this incoming stream open for a longer time, such as when downloading a very large stream or when streaming data over a long-lived connection.
If you're using a streaming request body, the time it takes to send the request body stream will not be included in the timeout. This allows you to keep this outgoing stream open for a longer time, such as when uploading a very large stream.
Note that this timeout handling applies to the higher-level HTTP layer. Lower
layers such as socket and DNS may also apply (different) timeout values. In
particular, the underlying socket connection uses the same default_socket_timeout
setting to establish the underlying transport connection. To control this
connection timeout behavior, you can inject a custom Connector
暂无开放 Issues,或尚未同步最近议题。