<?php
function msgProcess($userMsg){
if (mb_strpos($userMsg,"绑定诊卡")||mb_strpos($userMsg,"诊卡绑定")) {
$rebackStr = "请回复您的诊卡号进行身份绑定";
}elseif (mb_strpos($userMsg,"诊费查询",0,"UTF-8")||mb_strpos($userMsg,"查询诊费",0,"UTF-8")) {
$rebackTime = date("Y-m-d H:i:s",time());
$rebackStr = "您于".$rebackTime."有一笔200元诊费待交";
}elseif (mb_strpos($userMsg,"报告查询",0,"UTF-8")||mb_strpos($userMsg,"查询报告",0,"UTF-8")) {
$rebackStr = "您最新一次检查一切正常,祝君健康!";
}else {
$rebackStr = "明德E医祝君健康";
}
return $rebackStr;
}
?><?php
define('TOKEN','hzcc'); //定义通信私钥 token
if(isset($_GET['echostr'])) { //如果 echostr 存在,即为 token 验证阶段
checkSignature(); //回复 token 的函数
} else {
include("msgprocess.php"); // 引入消息处理文件
reciveMsg(); // 接收并处理消息
}
//接收微信发来的信息
function reciveMsg(){
$postStr = file_get_contents("php://input"); //获取微信服务器转发的消息包(xml 格式)
if(!empty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //将 xml 消息包转为对象
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$content = trim($postObj->Content);
$time = time();
$msgType = "text";
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if(!empty($content)) {
$contentStr = msgProcess($content);
$result = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $result;
exit;
} else {
echo "";
exit;
}
} else {
echo "";
exit;
}
}
//检查 signature 参数(验证身份签名)
function checkSignature(){
$echoStr = $_GET['echostr'];
$signature = trim($_GET["signature"]);
$timestamp = trim($_GET["timestamp"]);
$nonce = trim($_GET["nonce"]);
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce); //签名拼接
sort($tmpArr, SORT_STRING); //排序加密
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr); //sha1 加密
if($tmpStr == $signature) {
ob_clean();
echo $echoStr;
exit;
} else {
return false;
}
}
?>
最后修改时间:
评论已关闭