loadXML($xmltext); // 加载XML文本 // 使用DOMDocument对象的getElementsByTagName方法获取Encrypt和ToUserName标签 $array_e = $xml->getElementsByTagName('Encrypt'); $array_a = $xml->getElementsByTagName('ToUserName'); // 获取Encrypt标签的文本内容和ToUserName标签的文本内容 $encrypt = $array_e->item(0)->nodeValue; $tousername = $array_a->item(0)->nodeValue; // 返回提取结果,包括状态码0(表示成功),加密信息和接收方账号 return array(0, $encrypt, $tousername); } catch (\Exception $e) { // 如果发生异常,返回错误码和null值 return array(ErrorCode::$ParseXmlError, null, null); } } // 生成方法,用于生成XML格式的响应消息 public function generate($encrypt, $signature, $timestamp, $nonce) { // 定义XML格式的字符串模板,使用CDATA区包含加密信息和其他参数 $format = " %s "; // 使用sprintf函数格式化XML字符串模板,插入加密信息和其他参数 return sprintf($format, $encrypt, $signature, $timestamp, $nonce); } } ?>