get(IdGeneratorInterface::class); return $generator->generate(); } catch (\Throwable $e) { // 记录错误日志 error_log("Snowflake ID generation failed: " . $e->getMessage()); return false; } } public static function im_post($url, $data, $options = []) { // 初始化CURL会话 $ch = curl_init($url); // JSON 编码(保持中文不转义) $jsonBody = is_string($data) ? $data : json_encode($data, JSON_UNESCAPED_UNICODE); // 基础选项 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody); // HTTPS 兼容 if (stripos($url, 'https://') === 0) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } // Header:Content-Type JSON,Authorization(优先使用传入的) $authorization = $options['authorization'] ?? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI'; $headers = [ 'Content-Type: application/json', 'Accept: application/json', 'Authorization: ' . $authorization, ]; // 允许外部追加自定义 header(数组,形如 ['X-xxx: yyy']) if (!empty($options['headers']) && is_array($options['headers'])) { $headers = array_merge($headers, $options['headers']); } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 超时设置(可通过 options 覆盖) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int)($options['connect_timeout'] ?? 5)); curl_setopt($ch, CURLOPT_TIMEOUT, (int)($options['timeout'] ?? 15)); // 追加额外的 CURL 选项(如需) if (!empty($options['curl']) && is_array($options['curl'])) { curl_setopt_array($ch, $options['curl']); } // 执行请求 $response = curl_exec($ch); if ($response === false) { $errorMsg = curl_error($ch); curl_close($ch); throw new \Exception('CURL Error: ' . $errorMsg); } // 获取HTTP状态码 $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭会话 curl_close($ch); // 返回结果 return json_decode($response,true); } }