PFinal\Wechat\SDK\WXBizMsgCrypt::encryptMsg PHP Method

encryptMsg() public method

  1. 对要发送的消息进行AES-CBC加密
  2. 生成安全签名
  3. 将消息密文和安全签名打包成xml格式
public encryptMsg ( $replyMsg, $timeStamp, $nonce, &$encryptMsg ) : integer
$replyMsg string 公众平台待回复用户的消息,xml格式的字符串
$timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp
$nonce string 随机串,可以自己生成,也可以用URL参数的nonce
return integer 成功0,失败返回对应的错误码
    public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg)
    {
        $pc = new Prpcrypt($this->encodingAesKey);
        //加密
        $array = $pc->encrypt($replyMsg, $this->appId);
        $ret = $array[0];
        if ($ret != 0) {
            return $ret;
        }
        if ($timeStamp == null) {
            $timeStamp = time();
        }
        $encrypt = $array[1];
        //生成安全签名
        $sha1 = new SHA1();
        $array = $sha1->getSHA1($this->token, $timeStamp, $nonce, $encrypt);
        $ret = $array[0];
        if ($ret != 0) {
            return $ret;
        }
        $signature = $array[1];
        //生成发送的xml
        $xmlparse = new XMLParse();
        $encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce);
        return ErrorCode::$OK;
    }

Usage Example

Beispiel #1
0
 /**
  * 解密
  * @param $replyMsg
  * @param $timestamp
  * @param $nonce
  * @return string
  * @throws WechatException
  */
 protected function encryptMsg($replyMsg, $timestamp, $nonce)
 {
     $msg = '';
     //传入公众号第三方平台的token(申请公众号第三方平台时填写的接收消息的校验token), 公众号第三方平台的appid, 公众号第三方平台的 EncodingAESKey(申请公众号第三方平台时填写的接收消息的加密symmetric_key)
     $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
     //第三方收到公众号平台发送的消息
     $errCode = $pc->encryptMsg($replyMsg, $timestamp, $nonce, $msg);
     if ($errCode == 0) {
         return $msg;
     }
     throw new WechatException('encrypt msg error. error code ' . $errCode);
 }