request = $request; $this->response = $response; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { $req = $this->request->all(); $concatenated = ""; if($req){ foreach ($req as $value) { if (is_array($value)) { // 如果值是数组,将数组元素用逗号连接 $concatenated.= implode(',', $value); } else { // 如果不是数组,直接拼接 $concatenated.= $value; } } } $url = env("SENSITIVE_WORD")."?msg=".urlencode($concatenated); $rep = PublicData::http_get($url); if (preg_match('/\{[^{}]*"code":[^}]*\}/', $rep, $matches)) { $jsonString = $matches[0]; // 解析 JSON 字符串为 PHP 数组 $jsonData = json_decode($jsonString, true); if ($jsonData!== null) { // 输出解析后的 JSON 数据 print_r($jsonData); if($jsonData['code']==200){ if(isset($jsonData['num']) && $jsonData['num']>0){ return $this->response->json( [ 'code' => 0, 'data' => [], 'message' => "存在敏感词:".$jsonData['ci'], ] ); } } } else { return $this->response->json( [ 'code' => 0, 'data' => [], 'message' => '解析敏感词接口失败', ] ); } } return $handler->handle($request); }catch (\Exception $e){ return $this->response->json( [ 'code' => $e->getCode(), 'data' => [], 'message' => '敏感词检测失败:'.$e->getMessage(), ] ); } return false; } }