Exemplo n.º 1
0
假设企业需要回复用户的明文如下:
<xml>
<ToUserName><![CDATA[mycreate]]></ToUserName>
<FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
<AgentID>128</AgentID>
</xml>

为了将此段明文回复给用户,企业应:
1.自己生成时间时间戳(timestamp),随机数字串(nonce)以便生成消息体签名,也可以直接用从公众平台的post url上解析出的对应值。
2.将明文加密得到密文。
3.用密文,步骤1生成的timestamp,nonce和企业在公众平台设定的token生成消息体签名。
4.将密文,消息体签名,时间戳,随机数字串拼接成xml格式的字符串,发送给企业号。
以上2,3,4步可以用公众平台提供的库函数EncryptMsg来实现。
*/
// 需要发送的明文
$sRespData = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName><FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a test]]></Content><MsgId>1234567890123456</MsgId><AgentID>128</AgentID></xml>";
$sEncryptMsg = "";
//xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);
if ($errCode == 0) {
    // TODO:
    // 加密成功,企业需要将加密之后的sEncryptMsg返回
    // HttpUtils.SetResponce($sEncryptMsg);  //回复加密之后的密文
} else {
    print "ERR: " . $errCode . "\n\n";
    // exit(-1);
}
Exemplo n.º 2
0
 /**
  * 发送文本消息
  */
 public static function sendMsg($paramArr)
 {
     $options = array('toUserName' => '', 'msgContent' => '', 'msgType' => 'text');
     if (is_array($paramArr)) {
         $options = array_merge($options, $paramArr);
     }
     extract($options);
     $corpId = self::$corpId;
     $token = self::$token;
     $signature = self::$signature;
     $timestamp = self::$timestamp;
     $encodingAesKey = self::$encodingAesKey;
     $nonce = self::$nonce;
     $agentId = self::$agentId;
     if (empty($corpId)) {
         return false;
     }
     $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
     $expend = "";
     if ($msgContent) {
         switch ($msgType) {
             #文本
             case "text":
                 $expend = "<Content><![CDATA[{$msgContent}]]></Content>";
                 break;
                 #图片
             #图片
             case "image":
                 $expend = "<Image><MediaId><![CDATA[{$msgContent}]]></MediaId></Image>";
                 break;
                 #声音
             #声音
             case "voice":
                 $expend = "<Voice><MediaId><![CDATA[{$msgContent}]]></MediaId></Voice>";
                 break;
                 #新闻
             #新闻
             case "news":
                 if (!is_array($msgContent)) {
                     break;
                 }
                 $expend = "<Articles>";
                 foreach ($msgContent as $value) {
                     !empty($value['title']) && ($expend .= "<item><Title><![CDATA[{$value['title']}]]></Title>");
                     !empty($value['title']) && ($expend .= "<Description><![CDATA[{$value['desc']}]]></Description>");
                     !empty($value['title']) && ($expend .= "<PicUrl><![CDATA[{$value['picUrl']}]]></PicUrl>");
                     !empty($value['title']) && ($expend .= "<Url><![CDATA[{$value['url']}]]></Url></item>");
                 }
                 $expend .= "</Articles>";
                 break;
         }
     }
     $sRespData = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName><FromUserName><![CDATA[{$corpId}]]></FromUserName><CreateTime>" . SYSTEM_TIME . "</CreateTime><MsgType><![CDATA[text]]></MsgType>{$expend}</xml>";
     $sEncryptMsg = "";
     //xml格式的密文
     $errCode = $wxcpt->EncryptMsg($sRespData, $timestamp, $nonce, $sEncryptMsg);
     return $sEncryptMsg;
 }
Exemplo n.º 3
0
 /**
  * 企业号信息加密处理
  */
 protected function encrypt($msg)
 {
     $sEncryptMsg = "";
     //xml格式的密文
     $timestamp = time();
     $nonce = uniqid();
     $app = \TMS_APP::model('mp\\mpaccount')->byId($this->call['mpid']);
     $wxcpt = new WXBizMsgCrypt($app->token, $app->qy_encodingaeskey, $app->qy_corpid);
     $errCode = $wxcpt->EncryptMsg($msg, $timestamp, $nonce, $sEncryptMsg);
     if ($errCode != 0) {
         TMS_APP::model('log')->log($this->call['mpid'], $this->content, $errCode);
         exit;
     }
     return $sEncryptMsg;
 }