Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 29, 2019
2 parents ef6ccae + ea2d246 commit 67a42ee
Show file tree
Hide file tree
Showing 23 changed files with 1,414 additions and 339 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ API 文档:[https://apidoc.gitee.com/yurunsoft/YurunHttp](https://apidoc.gitee

> 每个小版本的更新日志请移步到 Release 查看
v3.3.0 新增支持 `Http2` 客户端
v3.4.0 新增支持 `Http2` 全双工用法

v3.3.0 新增支持 `Http2` 兼容用法

v3.2.0 新增支持 `Swoole WebSocket` 客户端

Expand All @@ -41,7 +43,7 @@ v1.0-1.3 初期版本迭代
```json
{
"require": {
"yurunsoft/yurun-http": "^3.3.0"
"yurunsoft/yurun-http": "^3.4.0"
}
}
```
Expand Down Expand Up @@ -126,7 +128,7 @@ go(function(){
});
```

### Http2 支持
### Http2 兼容用法

```php
$http = new HttpRequest;
Expand All @@ -142,6 +144,40 @@ Curl: `php --ri curl`

Swoole: `php --ri swoole`

### Http2 全双工用法

> 该用法仅支持 Swoole
```php
$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);
});
}
```

> 具体用法请看 `examples/http2Client.php`
## 捐赠

<img src="https://raw.githubusercontent.com/Yurunsoft/YurunHttp/master/res/pay.png"/>
Expand Down
17 changes: 17 additions & 0 deletions examples/http2-without-recv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* 简单用法示例
*/
require dirname(__DIR__) . '/vendor/autoload.php';

use Yurun\Util\HttpRequest;
use Yurun\Util\YurunHttp;
use Yurun\Util\YurunHttp\Handler\Swoole;

YurunHttp::setDefaultHandler(Swoole::class);

go(function(){
$http = new HttpRequest;
$http->protocolVersion = '2.0';
$http->sendHttp2WithoutRecv('https://wiki.swoole.com/', null, 'GET');
});
45 changes: 45 additions & 0 deletions examples/http2Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Http Client
*/

use Yurun\Util\HttpRequest;
use Swoole\Coroutine\Channel;
use Yurun\Util\YurunHttp\Http\Psr7\Uri;

require dirname(__DIR__) . '/vendor/autoload.php';

go(function(){
$uri = new Uri('https://www.taobao.com/');

$client = new \Yurun\Util\YurunHttp\Http2\SwooleClient($uri->getHost(), Uri::getServerPort($uri), 'https' === $uri->getScheme());
$client->connect();

$httpRequest = new HttpRequest;

$count = 10;
$channel = new Channel($count);
for($i = 0; $i < $count; ++$i)
{
go(function() use($i, $client, $channel, $httpRequest, $uri){
$request = $httpRequest->header('aaa', 'bbb')->buildRequest($uri, [
'date' => $i,
], 'POST', 'json');
$streamId = $client->send($request);
var_dump('send:' . $streamId);
$response = $client->recv($streamId, 3);
$content = $response->body();
var_dump('recv:' . $response->getStreamId() . ';statusCode:'. $response->getStatusCode(), ';contentLength:' . strlen($content));
$channel->push(1);
});
}
$returnCount = 0;
do {
if($channel->pop())
{
++$returnCount;
}
} while($returnCount < $count);

$client->close();
});
61 changes: 31 additions & 30 deletions examples/psr7Ex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/
require dirname(__DIR__) . '/vendor/autoload.php';

use Yurun\Util\YurunHttp\Http\Request;
use Yurun\Util\YurunHttp;
use Yurun\Util\YurunHttp\Attributes;
use Yurun\Util\YurunHttp\Http\Request;

test();

Expand All @@ -14,68 +15,68 @@ function test()
$url = 'http://www.baidu.com';

// 全局参数,一旦设置,所有请求都会使用该参数
// YurunHttp::setAttribute('customLocation', true);
// YurunHttp::swithAttribute(Attributes::CUSTOM_LOCATION, true);

$request = new Request($url);

// 以下参数都为可选,示例中的值为默认值
$request = $request
// 自定义重定向,仅Curl模式有效,一般不推荐使用。Swoole模式强制自定义重定向
->withAttribute('customLocation', false)
->withAttribute(Attributes::CUSTOM_LOCATION, false)
// 设置是否允许重定向。如果为true,则遇到301或302进行重定向操作。如果为false则不重定向。
->withAttribute('followLocation', true)
->withAttribute(Attributes::FOLLOW_LOCATION, true)
// 最大重定向次数,防止无限重定向卡死
->withAttribute('maxRedirects', 10)
->withAttribute(Attributes::MAX_REDIRECTS, 10)
// 失败重试次数
->withAttribute('retry', 0)
->withAttribute(Attributes::RETRY, 0)
// 是否验证证书
->withAttribute('isVerifyCA', false)
->withAttribute(Attributes::IS_VERIFY_CA, false)
// CA证书路径
->withAttribute('caCert', null)
->withAttribute(Attributes::CA_CERT, null)
// SSL证书路径
->withAttribute('certPath', '')
->withAttribute(Attributes::CERT_PATH, '')
// SSL证书密码,Swoole暂不支持
->withAttribute('certPassword', null)
->withAttribute(Attributes::CERT_PASSWORD, null)
// SSL证书类型,仅Curl有效,支持的格式有"PEM" (默认值), "DER"和"ENG"。Swoole仅支持PEM
->withAttribute('certType', 'pem')
->withAttribute(Attributes::CERT_TYPE, 'pem')
// SSL私钥路径
->withAttribute('keyPath', '')
->withAttribute(Attributes::KEY_PATH, '')
// SSL私钥密码,Swoole暂不支持
->withAttribute('keyPassword', null)
->withAttribute(Attributes::KEY_PASSWORD, null)
// SSL私钥类型,仅Curl有效,支持的格式有"PEM" (默认值), "DER"和"ENG"。Swoole仅支持PEM
->withAttribute('keyType', 'pem')
->withAttribute(Attributes::KEY_TYPE, 'pem')
// 扩展设置。用途:Curl:curl_setopt_array();Swoole:Client->set()
->withAttribute('options', [])
->withAttribute(Attributes::OPTIONS, [])
// 保存文件路径,不为null则将请求结果的body保存到文件
->withAttribute('saveFilePath', null)
->withAttribute(Attributes::SAVE_FILE_PATH, null)
// 保存文件读写模式
->withAttribute('saveFileMode', 'w+')
->withAttribute(Attributes::SAVE_FILE_MODE, 'w+')
// 是否使用代码
->withAttribute('useProxy', false)
->withAttribute(Attributes::USE_PROXY, false)
// 代理类型。Curl支持http、socks4、socks4a、socks5;Swoole支持http和socks5
->withAttribute('proxy.type', 'http')
->withAttribute(Attributes::PROXY_TYPE, 'http')
// 代理认证方式。Curl支持basic和ntlm;Swoole仅支持basic
->withAttribute('proxy.auth', 'basic')
->withAttribute(Attributes::PROXY_AUTH, 'basic')
// 代理服务器地址
->withAttribute('proxy.server', null)
->withAttribute(Attributes::PROXY_SERVER, null)
// 代理服务器端口
->withAttribute('proxy.port', null)
->withAttribute(Attributes::PROXY_PORT, null)
// 代理账号
->withAttribute('proxy.username', '')
->withAttribute(Attributes::PROXY_USERNAME, '')
// 代理密码
->withAttribute('proxy.password', '')
->withAttribute(Attributes::PROXY_PASSWORD, '')
// http认证的用户名
->withAttribute('username', null)
->withAttribute(Attributes::USERNAME, null)
// http认证的密码
->withAttribute('password', '')
->withAttribute(Attributes::PASSWORD, '')
// 连接超时时间,单位:毫秒,仅Curl有效
->withAttribute('connectTimeout', 30000)
->withAttribute(Attributes::CONNECT_TIMEOUT, 30000)
// 总超时时间,单位:毫秒
->withAttribute('timeout', 30000)
->withAttribute(Attributes::TIMEOUT, 30000)
// 下载限速,单位:字节,仅Curl有效
->withAttribute('downloadSpeed', 30000)
->withAttribute(Attributes::DOWNLOAD_SPEED, 30000)
// 上传限速,单位:字节,仅Curl有效
->withAttribute('uploadSpeed', 30000)
->withAttribute(Attributes::UPLOAD_SPEED, 30000)
;
$response = YurunHttp::send($request);
var_dump($response);
Expand Down
5 changes: 2 additions & 3 deletions examples/swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
use Yurun\Util\HttpRequest;

// 设置默认请求处理器为 Swoole
YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole'); // php 5.4
// YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole'); // php 5.4
// YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class); // php 5.5+

// Swoole 处理器必须在协程中调用
go('test');

function test()
{
$http = new HttpRequest;
$response = $http->get('http://www.baidu.com');
$response = $http->get('http://127.0.0.1:8901');
echo 'html:', PHP_EOL, $response->body();
}
19 changes: 18 additions & 1 deletion src/HttpRequest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Yurun\Util;

use Yurun\Util\YurunHttp\Http\Psr7\Consts\MediaType;
use Yurun\Util\YurunHttp\Attributes;
use Yurun\Util\YurunHttp\Http\Request;
use Yurun\Util\YurunHttp\Http\Psr7\UploadedFile;
use Yurun\Util\YurunHttp\Http\Psr7\Consts\MediaType;

class HttpRequest
{
Expand Down Expand Up @@ -729,6 +730,22 @@ public function send($url = null, $requestBody = null, $method = 'GET', $content
return YurunHttp::send($request, $this->handler);
}

/**
* 发送 Http2 请求不调用 recv()
* @param string $url 请求地址,如果为null则取url属性值
* @param array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
* @param array $method 请求方法,GET、POST等
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
* @return \Yurun\Util\YurunHttp\Http\Response
*/
public function sendHttp2WithoutRecv($url = null, $requestBody = null, $method = 'GET', $contentType = null)
{
$request = $this->buildRequest($url, $requestBody, $method, $contentType)
->withProtocolVersion('2.0')
->withAttribute(Attributes::HTTP2_NOT_RECV, true);
return YurunHttp::send($request, $this->handler);
}

/**
* GET请求
* @param string $url 请求地址,如果为null则取url属性值
Expand Down
9 changes: 9 additions & 0 deletions src/YurunHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ abstract class YurunHttp
*/
private static $attributes = [];

/**
* 版本号
*/
const VERSION = '3.4.0';

/**
* 设置默认处理器类
* @param string $class
Expand Down Expand Up @@ -68,6 +73,10 @@ public static function send($request, $handlerClass = null)
}
$handler->send($request);
$response = $handler->recv();
if(!$response)
{
return $response;
}
$response = $response->withTotalTime(microtime(true) - $time);
return $response;
}
Expand Down
Loading

0 comments on commit 67a42ee

Please sign in to comment.