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.3.0):
-
Added support for HTTP / 2.0
-
Repair connection multiplexing problem
$http = new HttpRequest; $http->protocolVersion = '2.0'; // 这句是关键 $response = $http->get('https://wiki.swoole.com/');
Curl, Swoole Handler support Http2, but it should be noted that all need to take to enable parameter Http2 compile time.
Check whether to support:
Curl: php --ri curl
Swoole: php --ri swoole
Major version update log
Each small version of the update log venue to Release View
v3.3.0 Added support for Http2
clients
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.2" } }
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 \ the Http \ the Request; use Yurun \ Util \ YurunHttp; $ URL = 'http://www.baidu.com'; // constructor defined: __ construct ($ uri = null , array headers = $ [], $ body = '', $ Method :: = RequestMethod the GET, Version $ = '1.1', Array Server $ = [], Cookies Array $ = [], Array $ Files = []) $ Request the request new new = ($ URL); // send a request and get a result $ YurunHttp :: send Response = ($ request); var_dump ($ Response);
Swoole mode coroutine
? <PHP use Yurun \ Util \ YurunHttp; use Yurun \ Util \ the HttpRequest; // Set default request processor Swoole YurunHttp :: setDefaultHandler ( 'Yurun \ Util \ YurunHttp \ Handler \ Swoole'); PHP 5.4 // // :: setDefaultHandler YurunHttp (\ Yurun \ Util \ YurunHttp \ Handler \ Swoole :: class); PHP 5.5+ // // Swoole processor must call the coroutine Go ( 'Test'); function Test () { $ HTTP the HttpRequest new new =; $ Response = $ http-> GET ( 'http://www.baidu.com'); echo 'HTML:', value is 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