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' => [
'peer_name' => "reply.lsmir2.com",
'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);
$result= $http_client->request($url);
if ($result->getStatusCode() != 200) {
return "statusCode error: " . $result->getStatusCode();
}
return $result->getBody()->getContents();
}
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 ---------------------------------------------
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]
可以试试在ssl里面添加这个配置
如果有效可提交pr
测试发现是因为swoole环境 stream_socket_enable_crypto() 与php的默认行为不一致,无法自动检测到cdn的peer_name,
像 @luoyue 说的那样,加一个
'peer_name' => 'reply.lsmir2.com'就好了。http-client v3.0.5 开始做了兼容,升级下
http-client 升级到 3.0.5 使用测试代码直接被阻塞了.没有返回 浏览一直等待状态
我这测试正常,还有你不要返回 return $http_client->request($url); ,webman要求返回字符串或者 support/Response 对象。
好的老板,问题解决了.