HttpClient的问题

anxin

问题描述

参考文档
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

操作系统及workerman/webman等框架组件具体版本

debian12 和 windows11/10都试了
截图

在config/process.php
截图

228 1 0
1个回答

加一句

$worker->eventLoop = \Workerman\Events\Fiber::class;
  • anxin 5天前

    这个加到什么位置呢,在webman里边使用

  • walkor 5天前

    config/process.php

  • anxin 5天前

    只能用\Workerman\Events\Fiber::class 吗? 用SWOW和http-client不行?

  • walkor 5天前

    可以,要用最新的v3 http-client版本

  • anxin 4天前

    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)

  • anxin 4天前

    我把http-client升级到3.0以上了, 然后就报这个错

  • walkor 4天前

    更新到3.0.1试下

  • anxin 4天前

    更新到3.0.1之后又是这个 Call to a member function getStatusCode() on null

  • walkor 4天前

    更新到3.0.2

  • anxin 4天前

    发起请求是可以了, 但是对方收不到数据, 用guzzle没问题,我再看看

×
🔝