您的位置:首页 > 移动开发 > 微信开发

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

2016-05-03 18:18 549 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: