微信第三方开发:处理消息和事件页面发送文字信息

<span style="font-size:18px;">include_once "../lib/wxBizMsgCrypt.php";
$wxData = include_once "../lib/wxConfig.php";

// 第三方发送消息给公众平台
$encodingAesKey = $wxData['SecretKey'];
$token = $wxData['Token'];
$appId = $wxData['AppId'];
$appSecret = $wxData['AppSecret'];

$timeStamp = empty ( $_GET ['timestamp'] ) ? '' : trim ( $_GET ['timestamp'] );  
$nonce = empty ( $_GET ['nonce'] ) ? '' : trim ( $_GET ['nonce'] );  
$msg_sign = empty ( $_GET ['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] ); 
$encrypt_type = empty ( $_GET ['encrypt_type'] ) ? "" : trim ( $_GET ['encrypt_type'] ); 
$encryptMsg = file_get_contents ( 'php://input' );  

$pc = new WXBizMsgCrypt ( $token, $encodingAesKey, $appId ); 

include_once "../lib/wxHelper.php";
$helper = new wxHelper();

$msg = "";
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg);

if ($errCode == 0) 
{
    $helper->set_php_file("../lib/static/component_verify_ticket_un.php", $msg);
    $data = $helper->xmlToArr ( $msg );
    $fromUsername = $data['FromUserName'];
    $toUsername = $data['ToUserName'];
    $keyword = trim($data['Content']);
    $time = time();
    $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
                </xml>"; 

    if(!empty( $keyword ))
    {
        $msgType = "text";
        $contentStr = "Welcome to wechat world!";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        $encryptMsg = '';
        $errCode = $pc->encryptMsg($resultStr, $timeStamp, $nonce, $encryptMsg);
        if ($errCode == 0) {
            echo $encryptMsg;
        } else {
            echo $errCode;
        }
    }else{
        echo "Input something...";
    }  
}else
{
    echo "not success";
}
</span>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值