企业号微信消息响应

<?php 
include_once "./demo/WXBizMsgCrypt.php";
// 假设企业号在公众平台上设置的参数如下
$encodingAesKey = "tRx1Mg798ImKu3N3YerBjKB5pJwNem1mfwv9BEDT4lz";
$token = "9gjZHnJ";
$corpId = "wx83f0d0a2b0d3d571";
url_valid($token,$encodingAesKey,$corpId);
decrypt_msg($token,$encodingAesKey,$corpId,$Content);



//公众号服务器数据url验证
function url_valid($token,$encodingAesKey,$corpId)
{
    $sVerifyMsgSig = $_GET[‘msg_signature‘];
    $sVerifyTimeStamp = $_GET[‘timestamp‘];
    $sVerifyNonce = $_GET[‘nonce‘];
    $sVerifyEchoStr = $_GET[‘echostr‘]; 
    $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); 
    if($sVerifyEchoStr)
    {
        $sEchoStr = "";
        $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
        if($errCode == 0) 
        {
            logger("R \r\n".$sEchoStr);//写入日志文件
            print($sEchoStr); 
        } else 
        {
            print($errCode . "\n\n");
        }
    }
}



//xml解密
function decrypt_msg($token,$encodingAesKey,$corpId)
{
    $sReqMsgSig = $_GET[‘msg_signature‘];
    $sReqTimeStamp = $_GET[‘timestamp‘];
    $sReqNonce = $_GET[‘nonce‘];
    $sReqData = $GLOBALS[‘HTTP_RAW_POST_DATA‘];

    $sMsg = "";  // 解析之后的明文
    //判断收到信息为真
    if($sReqData)
    {
        $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); 
        $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); //xml解密
        if($errCode == 0) 
        {
            $xml = new DOMDocument();
            $xml->loadXML($sMsg);
            $Content = $xml->getElementsByTagName(‘Content‘)->item(0)->nodeValue;//得到消息内容
            $MsyType = $xml->getElementsByTagName(‘MsgType‘)->item(0)->nodeValue;//得到消息类型
            $FromUserName = $xml->getElementsByTagName(‘FromUserName‘)->item(0)->nodeValue; //发送用户的id
            $MsgType = $xml->getElementsByTagName(‘MsgType‘)->item(0)->nodeValue; //消息类型
            $CreateTime = $xml->getElementsByTagName(‘CreateTime‘)->item(0)->nodeValue;
            $MsgId = $xml->getElementsByTagName(‘MsgId‘)->item(0)->nodeValue;
            $AgentID = $xml->getElementsByTagName(‘AgentID‘)->item(0)->nodeValue; 
            logger("R \r\n".$sMsg);//写入日志文件
            encrypt_msg($sReqNonce,$token,$encodingAesKey,$corpId,$Content);

        }else 
        {
            print("ERR: " . $errCode . "\n\n");
        }
    }

}





//重新加密xml数据
function encrypt_msg($sReqNonce,$token,$encodingAesKey,$corpId,$Content)
{
    $sRespData ="<xml><ToUserName><![CDATA[".$FromUserName."]]></ToUserName>
                <FromUserName><![CDATA[wx83f0d0a2b0d3d571]]></FromUserName>
                <CreateTime>1348831860</CreateTime><MsgType>
                <![CDATA[text]]></MsgType>
                <Content><![CDATA[".$Content."]]></Content>
                <MsgId>1234567890123456</MsgId>
                <AgentID>128</AgentID>
            </xml>";
    $sEncryptMsg = ""; //xml格式的密文  
    $sReqTimeStamp=time();
    $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); 
    $errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);  
    if($errCode == 0) 
    {
        
        print($sEncryptMsg);
        logger("R \r\n".$sEncryptMsg);//写入日志文件
    }else 
    {  
        print($errCode . "\n\n"); 
    }

}



//写日志

function logger($log_content)
{
    $max_size = 1000000;
    $log_filename = "log.xml";
    if( file_exists($log_filename) && (abs(filesize($log_filename))>$max_size) )
    {
        unlink($log_filename);
    }
    file_put_contents($log_filename, date(‘Y-m-d H:i:s‘)." ".$log_content."\r\n", FILE_APPEND);

}







 
?>

 

企业号微信消息响应

上一篇:js 微信分享


下一篇:微信公众号开发系列-微信企业号开发相关參数