参考文档
https://www.workerman.net/doc/workerman/components/workerman-http-client.html
协程用法
协程用法需要workerman>=5.0,workerman/http-client>=2.0.0 并安装 composer require revolt/event-loop
public static function request($cmd, $postData)
{
foreach ($postData as $index => $postDatum) {
if ($postDatum == '') {
unset($postData[$index]);
}
}
$reqBean = [
'orgId' => config('pay.tq.appid'),
'reqData' => $postData,
'reqId' => session_create_id(),
'signType' => 'RSA',
'timestamp' => date('ymdHis', time()),
'version' => '1.0',
];
$sign = self::generateSign($reqBean);
$reqBean['sign'] = $sign;
$client = new Client();
try {
$apiUrl = config('pay.tq.api') . $cmd;
$response = $client->post($apiUrl, $reqBean);
if ($response->getStatusCode() !== 200) {
debug_log('状态不对', $reqBean, $response->getStatusCode(), 'tq');
throw new BusinessException('EQT-ServerStatus响应错误');
}
$content = json_decode($response->getBody()->getContents(), true);
if ($content['bizCode'] !== '0000') {
debug_log($cmd, $reqBean, $content, 'tq');
throw new BusinessException($cmd . '@' . $content['bizMsg']);
}
return $content;
} catch (\Throwable $e) {
debug_log($cmd, $reqBean, $e->getMessage(), 'tq');
throw new BusinessException($e->getMessage());
}
}
Call to a member function getStatusCode() on null
debian12 和 windows11/10都试了
在config/process.php
加一句
这个加到什么位置呢,在webman里边使用
config/process.php
只能用\Workerman\Events\Fiber::class 吗? 用SWOW和http-client不行?
可以,要用最新的v3 http-client版本
Fatal error: [Fatal error in R8] Cannot declare class Workerman\Coroutine, because the name is already in use
Stack trace:
0 D:\webman\webman-pay-api\vendor\composer\ClassLoader.php(576): include()
1 D:\webman\webman-pay-api\vendor\composer\ClassLoader.php(427): Composer\Autoload{closure}('D:\webman\webma...')
2 D:\webman\webman-pay-api\vendor\workerman\http-client\src\Client.php(77): Composer\Autoload\ClassLoader->loadClass('Workerman\Corou...')
3 D:\webman\webman-pay-api\vendor\workerman\http-client\src\Client.php(153): Workerman\Http\Client->request('https://openapi...', Array)
我把http-client升级到3.0以上了, 然后就报这个错
更新到3.0.1试下
更新到3.0.1之后又是这个 Call to a member function getStatusCode() on null
更新到3.0.2
发起请求是可以了, 但是对方收不到数据, 用guzzle没问题,我再看看