http-client 请求 cf保护的域名 has been closed.

lsmir2

使用http-client请求 开启 cloudflare DNS 域名 返回 has been closed.

The connection to reply.lsmir2.com has been closed. in /Volumes/code/webman/vendor/workerman/http-client/src/Request.php:547

程序代码或配置

public function index(Request $request): mixed
    {
        $url = "https://reply.lsmir2.com/reply.php"; //域名套了CF
        $options = [
            'max_conn_per_addr' => 100,// 每个域名最多维持多少并发连接
            'keepalive_timeout' => 15,// 连接多长时间不通讯就关闭
            'connect_timeout' => 30,// 连接超时时间
            'timeout' => 30,// 请求发出后等待响应的超时时间
            'method' => 'POST',
            'version' => '1.1',
            'headers' => ['Content-Type' => 'application/json'],
            'data' =>  json_encode(["id" => 1]) ,
            'context' => [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true,
                    'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT,
                ],
            ],
        ];
        $http_client = new  Client($options);
        return $http_client->request($url);
    }

使用curl才正常

     public function index(Request $request): mixed
    {
        $url = "https://reply.lsmir2.com/reply.php";
        return $this->curl($url, ["id" => 1]);
    }
   private function curl(string $url, array $data, ?callable $successCallback = null, ?callable $errorCallback = null): mixed
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));//json
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);  //设置HTTP头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   // 添加SSL证书验证配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        $result = curl_exec($ch);
        if ($result === false) {
            $error = curl_error($ch);
            // 调用错误回调
            if ($errorCallback) $errorCallback($error);
            return " curl_exec error: $error ";
            //运营商拦截会返回 Connection reset by peer  本地环境增加代理即可,生产环境不会有这个错误
        }
        try {
            if ($successCallback) $successCallback($result);
        } catch (Throwable $e) {
            if ($errorCallback) $errorCallback($result);
        }
        curl_close($ch);
        return $result;
    }

操作系统环境及workerman/webman等具体版本

-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman/5.1.3 PHP/8.4.12 (JIT off) Darwin/23.6.0
--------------------------------------------- WORKERS ----------------------------------------------
event-loop proto user worker listen count state
swoole tcp lsmir2 webman http://0.0.0.0:8787 1 [OK]
event tcp lsmir2 monitor none 1 [OK]

46 0 0
0个回答

🔝