18910140161

微信公众平台自动回复、关注等事件监听

顺晟科技

2021-06-16 10:56:40

860

?服务器端编程语言(专业超文本预处理器的缩写)

//明文方式

定义(' TOKEN ',' XXX ');

类消息{

私有$ fromUsername

private $ toUsername

' private $ msgType='文本

//关注、取关、消息接收

公共函数索引(){

if ($_GET['echostr']) {

$ this-valid();//如果发来了数据则进行验证

} else {

$ this-response msg();//如果没有echostr,则返回消息

}

}

私人函数帖子($url,$data) {

$opts=array('http'=array(

方法='开机自检',

header '=array(' Content-type : application/x-www-form-URL encoded ',' response-js : true '),

内容=$data,

),

);

$ context=stream _ context _ create($ opts);

$ result=JSON _ decode(file _ get _ contents($ URL,false,$context),true);

返回$结果

}

私有函数有效(){

//有效签名,选项

$ echoStr=$ _ GET[' echoStr '];

if ($this-checkSignature()) {

//调用验证字段

echo $ echoStr

退出;

}

}

私有函数responseMsg() {

//获取帖子数据,可能是由于环境不同

//$ PostStr=$ GLOBALS[' HTTP _ RAW _ POST _ DATA '];//接收微信发来的可扩展置标语言数据

$ PostStr=$ PostStr=file _ get _ contents(' PHP ://input ');//接收微信发来的可扩展置标语言数据

//提取帖子数据

if(!空($postStr)) {

//解析邮政来的可扩展置标语言为一个对象$postObj

$ PostObj=simple XML _ load _ string($ PostStr,' SimpleXMLElement ',libXML _ NocDATa);

$ PostObj=JSON _ decode(JSON _ encode($ PostObj),true);

$ this-FromUserName=$ PostObj[' FromUserName '];//请求消息的用户

$ this-TouseName=$ PostObj[' TouseName '];//'我'的公众号编号

//$ keyword=trim($ PostObj[' Content ']);//消息内容

if($ PostObj[' MSgtype ']=' event '){

//如果可扩展置标语言信息里消息类型为事件

if($ PostObj[' Event ']=' subscribe '){

//如果是订阅事件

if(is _ string($ PostObj[' EventKey ']){

//通过场景码来的

}

$contentStr='欢迎您关注XXX ';

$ this-SendMeSH($ ContentStr);

} else if(' SCAN '==$ PostObj[' Event ']){

//已关注的扫码事件

if ($postObj['EventKey']) {

//通过场景码来的

}

} else if($ PostObj[' Event ']==' unsubscribe '){

//取关事件

}

}

if(' text '==$ PostObj[' MsGType ']){

$ this-SendMesh('这里是xxx!欢迎您的到来');

} else {

回声"";

退出;

}

} else {

回声"";

退出;

}

}

//验证字段

私有函数checkSignature() {

$ signature=$ _ GET[' signature '];

$ timestamp=$ _ GET[' timestamp '];

$ nonce=$ _ GET[' nonce '];

$ token=TOKEN

$tmpArr=array($token,$timestamp,$ nonce);

sort($ TMParr);

$tmpStr=内爆($ TMParr);

$ TMPstr=sha1($ TMPstr);

if ($tmpStr==$signature) {

返回真实的

} else {

返回错误的

}

}

私有函数sendmail($ content){

//返回消息模板

$textTpl='xml

来自用户名![CDATA[%s]]/FromUserName

创建时间% s/创建时间

MsgType![CDATA[%s]]/MsgType

内容![CData[% s]]/内容

/XML ';

$resultStr=sprintf($textTpl,$this-fromUsername,$this-toUsername,time(),$this-msgType,$ content);

echo $ resultStr

exit();

}

}

我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航