PHP code example of yurunsoft / yurun-http
1. Go to this page and download the library: Download yurunsoft/yurun-http library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yurunsoft / yurun-http example snippets
use Yurun\Util\HttpRequest;
$http = new HttpRequest;
// 设置 Header 4 种方法
$http->header('aaa', 'value1')
->headers([
'bbb' => 'value2',
'ccc' => 'value3',
])
->rawHeader('ddd:value4')
->rawHeaders([
'eee:value5',
'fff:value6',
]);
// 请求
$response = $http->ua('YurunHttp')
->get('http://www.baidu.com');
echo 'html:', PHP_EOL, $response->body();
use \Yurun\Util\YurunHttp\Co\Batch;
use \Yurun\Util\HttpRequest;
$result = Batch::run([
(new HttpRequest)->url('https://www.imiphp.com'),
(new HttpRequest)->url('https://www.yurunsoft.com'),
]);
var_dump($result[0]->getHeaders(), strlen($result[0]->body()), $result[0]->getStatusCode());
var_dump($result[1]->getHeaders(), strlen($result[1]->body()), $result[1]->getStatusCode());
use Yurun\Util\YurunHttp;
use Yurun\Util\HttpRequest;
// Swoole 处理器必须在协程中调用
go('test');
function test()
{
$http = new HttpRequest;
$response = $http->get('http://www.baidu.com');
echo 'html:', PHP_EOL, $response->body();
}
\Yurun\Util\YurunHttp\ConnectionPool::enable();
\Yurun\Util\YurunHttp\ConnectionPool::disable();
// 最大连接数=16个,连接数满等待超时时间(仅 Swoole 有效)=30s
// url 最后不要带斜杠 /
\Yurun\Util\YurunHttp\ConnectionPool::setConfig('https://imiphp.com', 16, 30);
$http = new HttpRequest;
$http->connectionPool(false);
use Yurun\Util\YurunHttp\Handler\Curl\CurlHttpConnectionManager;
use Yurun\Util\YurunHttp\Handler\Swoole\SwooleHttpConnectionManager;
// 获取 Curl 连接池管理器,选择你所处环境对应的类,其实一般 Curl 不太需要连接池
// $manager = CurlHttpConnectionManager::getInstance();
// 获取 Swoole 连接池管理器,选择你所处环境对应的类
$manager = SwooleHttpConnectionManager::getInstance();
// 获取连接池对象集合
$pool = $manager->getConnectionPool('https://imiphp.com');
// 获取连接总数
$pool->getCount();
// 获取空闲连接总数
$pool->getFree();
// 获取正在使用的连接总数
$pool->getUsed();
// 获取连接池配置
$config = $pool->getConfig();
go(function(){
$url = 'ws://127.0.0.1:1234/';
$http = new HttpRequest;
// 启用压缩
// $http->websocketCompression(true);
$client = $http->websocket($url);
if(!$client->isConnected())
{
throw new \RuntimeException('Connect failed');
}
// 文本
$client->send('data');
// 二进制
$client->send('data', \Yurun\Util\YurunHttp\WebSocket\Opcode::BINARY);
// 更多 Opcode 预定义在:\Yurun\Util\YurunHttp\WebSocket\Opcode
// 也支持 WebSocket 标准中的其它 Opcode
$recv = $client->recv();
var_dump('recv:', $recv);
$client->close();
});
$http = new HttpRequest;
$http->protocolVersion = '2.0'; // 这句是关键
$response = $http->get('https://wiki.swoole.com/');
$uri = new Uri('https://wiki.swoole.com/');
// 客户端初始化和连接
$client = new \Yurun\Util\YurunHttp\Http2\SwooleClient($uri->getHost(), Uri::getServerPort($uri), 'https' === $uri->getScheme());
$client->connect();
// 请求构建
$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){
// 发送(支持在多个协程执行)
$streamId = $client->send($request);
var_dump('send:' . $streamId);
// 接收(支持在多个协程执行)
$response = $client->recv($streamId, 3);
$content = $response->body();
var_dump($response);
});
}
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);
// 发送请求并获取结果
$response = YurunHttp::send($request);
var_dump($response);