/**
* 构造响应xml字符串,用于微信服务器请求时被动响应
* @param mixed $reply 被动响应的内容,ReplyMessage对象,News数组 或string
* @return string
* @throws WechatException
*/
public function buildReply($reply)
{
//回复的消息为null,会返回给微信一个"success",微信服务器不会对此作任何处理,并且不会发起重试
if (is_null($reply)) {
return 'success';
}
if (is_string($reply) || is_numeric($reply)) {
$reply = new Text("{$reply}");
}
//索引数组,多图文消息
if (is_array($reply) && array_keys($reply) === range(0, count($reply) - 1)) {
if ($reply[0] instanceof News) {
$reply = new News($reply);
}
}
if (!$reply instanceof ReplyMessage) {
throw new WechatException('Argument $reply must implement interface PFinal\\Wechat\\Contract\\ReplyMessage');
}
//手动构建好的xml字符串
if (strtolower($reply->type()) === 'raw') {
return $this->attemptEncrypt($reply->xmlData());
}
$data = array('ToUserName' => $this->getMessage()->FromUserName, 'FromUserName' => $this->getMessage()->ToUserName, 'CreateTime' => time(), 'MsgType' => $reply->type());
$data = array_merge($data, $reply->xmlData());
$data = Xml::build($data);
return $this->attemptEncrypt($data);
}