Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanzhihai committed Nov 14, 2023
1 parent 0a11332 commit 02bcf47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,26 @@ $response = Http::withResponseMiddleware(
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;

Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json', ['token' => TOKEN], function (Response $response) {
$promise = Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json', ['token' => TOKEN], function (Response $response) {
echo '异步请求成功,响应内容:' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
echo '异步请求异常,错误码:' . $e->getCode() . ',错误信息:' . $e->getMessage() . PHP_EOL;
});

$promise->wait();
echo json_encode(['code' => 200, 'msg' => '请求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;

//输出
{"code":200,"msg":"请求成功"}
异步请求成功,响应内容:{"code":200,"msg":"success","second":3}

Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep3.json', function (Response $response) {
$promise = Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep3.json', function (Response $response) {
echo '异步请求成功,响应内容:' . $response->body() . PHP_EOL;
}, function (RequestException $e) {
echo '异步请求异常,错误信息:' . $e->getMessage() . PHP_EOL;
});

$promise->wait();
echo json_encode(['code' => 200, 'msg' => '请求成功'], JSON_UNESCAPED_UNICODE) . PHP_EOL;

//输出
Expand All @@ -297,6 +301,9 @@ Http::deleteAsync(...);
Http::headAsync(...);

Http::optionsAsync(...);

使用 等待异步回调处理完成
Http::wait();
```

#### 异步并发请求
Expand All @@ -311,12 +318,15 @@ $promises = [
Http::postAsync('http://easyhttp.yzh52521.cn/api/sleep2.json', ['name' => 'yzh52521']),
];

Http::concurrency(10)->multiAsync($promises, function (Response $response, $index) {
$pool=Http::concurrency(10)->multiAsync($promises, function (Response $response, $index) {
echo "发起第 $index 个异步请求,请求时长:" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e, $index) {
echo "发起第 $index 个请求失败,失败原因:" . $e->getMessage() . PHP_EOL;
});

$promise = $pool->promise();
$promise->wait();

//输出
发起第 1 个请求失败,失败原因:cURL error 1: Protocol "http1" not supported or disabled in libcurl (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
发起第 2 个异步请求,请求时长:2 秒
Expand Down
4 changes: 1 addition & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public function __construct()
*/
public function __destruct()
{
if (!empty($this->promises)) {
//\GuzzleHttp\Promise\Utils($this->promises)->wait();
}

}

/**
Expand Down

0 comments on commit 02bcf47

Please sign in to comment.