/** * 生成用于回复的数据 * * @return array */ public function buildForReply() { if (!method_exists($this, 'toReply')) { throw new \Exception(__CLASS__ . '未实现此方法:toReply()'); } $base = array('ToUserName' => $this->to, 'FromUserName' => $this->from, 'CreateTime' => time(), 'MsgType' => $this->getDefaultMessageType()); return XML::build(array_merge($base, $this->toReply())); }
/** * 将公众平台回复用户的消息加密打包. * <ol> * <li>对要发送的消息进行AES-CBC加密</li> * <li>生成安全签名</li> * <li>将消息密文和安全签名打包成xml格式</li> * </ol> * * @param string $xml 公众平台待回复用户的消息,xml格式的字符串 * @param string $nonce 随机串,可以自己生成,也可以用URL参数的nonce * @param int $timestamp 时间戳,可以自己生成,也可以用URL参数的timestamp * * @return string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, * nonce, encrypt的xml格式的字符串 */ public function encryptMsg($xml, $nonce = null, $timestamp = null) { $encrypt = $this->encrypt($xml, $this->appId); !is_null($nonce) || ($nonce = substr($this->appId, 0, 10)); !is_null($timestamp) || ($timestamp = time()); //生成安全签名 $signature = $this->getSHA1($this->token, $timestamp, $nonce, $encrypt); $response = array('Encrypt' => $encrypt, 'MsgSignature' => $signature, 'TimeStamp' => $timestamp, 'Nonce' => $nonce); //生成响应xml return XML::build($response); }