json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE); } public static function echoError($msg){ $result=array(); $result['status']='no'; $result['msg']=$msg; return $result; } public static function echoError1($msg){ $result=array(); $result['status']=false; $result['msg']=$msg; return $result; } public static function Return($status,$msg,$data=[]){ $result=array(); $result['status']=$status; $result['msg']=$msg; $result['data']=$data; return $result; } public static function XMLReturn($SourceSystem,$MessageID,$code,$msg) { // 准备数据 $data = [ 'Response' => [ 'Header'=>[ 'SourceSystem'=>$SourceSystem, 'MessageID'=>$MessageID, ], 'Body' => [ 'ResultCode' => $code, 'ResultContent' => $msg, 'SuccessIDList' => [1,2,4], // 或者使用空数组 [] ], ], ]; // 将数据转换为 XML $xml = simplexml_load_string('' . ""); // array_walk_recursive($data, function($value, $key) use ($xml) { // $child = $xml->addChild($key, $value); // }); // 递归添加数据到 XML self::addNode($xml, $data); $xmlString = $xml->asXML(); // 设置响应头为 XML 并返回 return response($xmlString)->header('Content-Type', 'text/xml'); } private static function addNode(SimpleXMLElement $xml, array $data) { foreach ($data as $key => $value) { if (is_array($value)) { // 如果值是一个数组,则创建一个新节点并递归调用 $node = $xml->addChild($key); self::addNode($node, $value); } else { // 否则直接添加节点 $xml->addChild($key, $value); } } } }