YurunHttp is open source PHP HTTP library, support chaining, easy to use.
Supports all common GET, POST, PUT, DELETE, UPDATE, etc. request, support Http2, WebSocket, Cookies browser level management, uploading and downloading, set and read header, Cookie, request parameters, failure retry, speed limit, agent and certificates.
git repository directory is sample code examples!
Update log (v3.4.0):
New
-
Added support for full-duplex Http2 client
-
Defines the attributes of the new class Attributes, Attribute all operations using a constant as the name of the class
-
The connection manager is introduced Swoole Handler
-
Add Default Default Default UserAgent (can be specified in each request, or set global
Attribute::USER_AGENT
used instead of the default UA) -
Support request sent Http2 not call recv ()
optimization
-
Under Attribute default in the Request object, the class will get from YurunHttp
-
Reconstruction Swoole Handler
Http2 full-duplex usage
The use of only supports Swoole
$uri = new Uri('https://wiki.swoole.com/'); // initialize and client connection $client = new \Yurun\Util\YurunHttp\Http2\SwooleClient($uri->getHost(), Uri::getServerPort($uri), 'https' === $uri->getScheme()); $client->connect(); // request construction $httpRequest = new HttpRequest; $request = $httpRequest->header('aaa', 'bbb')->buildRequest($uri, [ 'date' => $i, ], 'POST', 'json'); for($i = 0; $i < 10; ++$i) { go(function() use($client, $request){ // Send (coroutine execution plurality of support) $streamId = $client->send($request); var_dump('send:' . $streamId); // receive (support multiple coroutine execution) $response = $client->recv($streamId, 3); $content = $response->body(); var_dump($response); }); }
See the specific usage
examples/http2Client.php
Major version update log
Each small version of the update log venue to Release View
v3.4.0 Added support for Http2
full-duplex usage
v3.3.0 Added support for Http2
compatible usage
v3.2.0 Added support for Swoole WebSocket
clients
v3.1.0 introduced the browser level Cookies
management
v3.0.0 Added support for Swoole
coroutine
v2.0.0 black history, do not tell you
v1.3.1 support Composer
The initial version of iterations v1.0-1.3
Composer
The project can use composer installed, follow psr-4 automatic loading rules in your composer.json
add the following content
{ "require": { "yurunsoft/yurun-http": "^3.4.0" } }
And then perform composer update
the installation.
After that you can use include "vendor/autoload.php";
to automatically load classes. (Ps: Do not forget namespace)
usage
Simple call
<?php use Yurun\Util\HttpRequest; $http = new HttpRequest; $response = $http->ua('YurunHttp') ->get('http://www.baidu.com'); echo 'html:', PHP_EOL, $response->body();
PSR-7 request construction
<?php use Yurun\Util\YurunHttp\Http\Request; use Yurun \ Util \ YurunHttp; $url = 'http://www.baidu.com'; // 构造方法定义:__construct($uri = null, array $headers = [], $body = '', $method = RequestMethod::GET, $version = '1.1', array $server = [], array $cookies = [], array $files = []) $request = new Request($url); // send the request and obtain results $response = YurunHttp::send($request); var_dump($response);
Swoole mode coroutine
<?php use Yurun \ Util \ YurunHttp; use Yurun\Util\HttpRequest; // Set default request processor Swoole YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole'); // php 5.4 // YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class); // php 5.5+ // Swoole processor must call the coroutine go('test'); function test() { $http = new HttpRequest; $response = $http->get('http://www.baidu.com'); echo 'html:', PHP_EOL, $response->body(); }
WebSocket Client
YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class); go(function(){ $ Url = 'ws: //127.0.0.1: 1234 /'; $http = new HttpRequest; $client = $http->websocket($url); if(!$client->isConnected()) { throw new \RuntimeException('Connect failed'); } $client->send('data'); $recv = $client->recv(); var_dump ( "recv: '$ recv); $client->close(); });
See specific examples
examples of code directory
Related Address:
- API documentation: https://apidoc.gitee.com/yurunsoft/YurunHttp
- Gitee: https://gitee.com/yurunsoft/YurunHttp
- Github:https://github.com/Yurunsoft/YurunHttp