http-client提示【域名】has been closed

dgkerry

问题描述

我使用Workerman\Http\Client来对外请求(并开启了协程),我发现在请求个别网站时会提示has been closed,我在本站和google上搜索与此有关的问题,有些人说ssl问题,于是我Workerman\Http\ConnectionPool的create中记录是否关闭ssl,事实上已关闭,我尝试写一个普通的curl来请求,却可以拿到数据,请问是什么回事呢

程序代码

$http = new Client();
$option=[
   'method' => 'get',
   'version' => '1.1',
];
$testnet=$http->request('【域名】',$option);

ssl关闭

#ConnectionPool 的create
protected function create($address, bool $ssl = false, string $proxy = ''): AsyncTcpConnection
    {
        $log = Log::channel('log2');//<----
        $context = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true
            ],
            'http' => [
                'proxy' => $proxy,
            ]
        ];
        if (!empty( $this->options['context'])) {
            $context = array_merge($context, $this->options['context']);
        }
        if (!$ssl) {
            unset($context['ssl']);
        }
        if (empty($proxy)) {
            unset($context['http']['proxy']);
        }
        if (!class_exists(Worker::class) || is_null(Worker::$globalEvent)) {
            throw new Exception('Only the workerman environment is supported.');
        }
        $log->info('create:'.json_encode($context));
        $connection = new AsyncTcpConnection($address, $context);
        if ($ssl) {
            $connection->transport = 'ssl';//<----
        }
        ProxyHelper::setConnectionProxy($connection, $context);
        $connection->address = ProxyHelper::addressKey($address, $proxy);
        $connection->connect();
        $connection->pool = ['connect_time' => time()];
        return $connection;
    }

    ##日志结果:[2025-04-07 18:58:59] log2.INFO: create:{"ssl":{"verify_peer":false,"verify_peer_name":false,"allow_self_signed":true},"http":[]}

报错信息

RuntimeException: The connection to 【域名】 has been closed. in /www/wwwroot/【项目】/vendor/workerman/http-client/src/Request.php:551
Stack trace:
#0 /www/wwwroot/【项目】/vendor/workerman/workerman/src/Connection/TcpConnection.php(1062): Workerman\Http\Request->onUnexpectClose()
#1 /www/wwwroot/【项目】/vendor/workerman/workerman/src/Connection/TcpConnection.php(874): Workerman\Connection\TcpConnection->destroy()
#2 /www/wwwroot/【项目】/vendor/workerman/workerman/src/Connection/AsyncTcpConnection.php(398): Workerman\Connection\TcpConnection->doSslHandshake()
#3 /www/wwwroot/【项目】/vendor/workerman/workerman/src/Events/Swoole.php(288): Workerman\Connection\AsyncTcpConnection->checkConnection()
#4 [internal function]: Workerman\Events\Swoole->Workerman\Events\{closure}()
#5 {main}

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

linux
-------composer.json--------
workerman/http-client ^3.0
workerman/webman-framework ^2.1

117 1 1
1个回答

walkor 打赏

使用swoole访问https地址需要swoole开启openssl。
另外,如果使用了swoole,就没必要使用http-client了,curl就可以了

  • 暂无评论
×
🔝