Skip to content

Commit

Permalink
Merge pull request #138 from wonder-game/develop
Browse files Browse the repository at this point in the history
feat: 优化request
  • Loading branch information
linkunyuan authored Dec 20, 2024
2 parents cb6f86b + 6577445 commit 6d2c5c8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Common/Classes/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function request($type, $url = '', $data = [], $method = 'post', $header
// 默认配置
$defaults = [
'resultType' => 'json',
'retryCallback' => function ($code = 200, $result = '') {
'retryCallback' => function ($code = 200, $result = [], $org = '') {
return in_array($code, [200, 302, 303, 307]);
},
'retryTimes' => 3,
Expand Down Expand Up @@ -61,27 +61,35 @@ public function request($type, $url = '', $data = [], $method = 'post', $header
}

if ($response instanceof Response) {
$res = $response->getBody();
$org = $response->getBody();
$code = $response->getStatusCode();
} else {
$res = $response['response'];
$org = $response['response'];
$code = $response['code'];
}

switch ($cfg['resultType']) {
case 'xml':
$res = $this->xml($res);
$res = $this->xml($org);
break;

case 'json':
$res = json_decode($res, true);
$res = json_decode($org, true);
break;

case 'body':
default:
$res = $org;
break;
}

// 自动重试
if (is_callable($cfg['retryCallback']) && ! $cfg['retryCallback']($code, $res)) {
if (is_callable($cfg['retryCallback']) && ! $cfg['retryCallback']($code, $res, $org)) {
if ($cfg['curt'] < $cfg['retryTimes']) {
Coroutine::sleep(0.5);
return $this->request($type, $url, $data, $method, $header, ['curt' => ++$cfg['curt']] + $cfg, $option);
}
$err = "{$url}响应失败!状态码为:{$code} 传参为:" . json_encode(func_get_args());
$err = "{$url}响应失败!状态码为:$code,响应内容为:$org, 传参为:" . json_encode(func_get_args());
trace($err, 'error');
throw new Exception($err);
}
Expand Down

0 comments on commit 6d2c5c8

Please sign in to comment.