YurunHttp is open source PHP HTTP library, support chaining, easy to use.
Supports all common GET, POST, PUT, DELETE, UPDATE, etc. request, to support uploading and downloading, set and read header, Cookie, request parameters, failure retry, speed limit, agents, and other certificates.
Perfect version 3.0 supports Curl, Swoole coroutine; version 3.2 supports Swoole WebSocket client.
git repository test directory is sample code!
Update log (v3.2.0):
- New WebSocket Client Support
WebSocket Client Swoole coroutine only for the environment.
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();
});
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