小程序模板网

微信小程序消息推送php服务器验证

发布时间:2018-04-16 11:01 所属栏目:小程序开发教程

作者:zhuwansan,来自授权地址 
微信文档(靠下有个“接入指引”):https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html

设置页面(“设置”>>“开发设置”): 
https://mp.weixin.qq.com/wxopen/initprofile?action=home&lang=zh_CN

 

设置服务器域名

比如:https://hosts.com 
注意http和https协议的不同。

 

设置消息推送

2.1 在你的服务器里添加服务器接口test.php,test.php接口内容主要是通过token验证消息是否为微信发来的,代码参照官方的例子:

 

				
  1. define("TOKEN","xxxxx");/ 后台填写的token
  2. $wechatObj = new wechatAPI();
  3. $wechatObj->isValid();
  4. class wechatAPI
  5. {
  6. public function isValid()//验证微信接口,如果确认是微信就返回它传来的echostr参数
  7. {
  8. $echoStr = $_GET["echostr"];
  9. if ($this->checkSignature()) {
  10. echo $echoStr;
  11. exit;
  12. }
  13. }
  14. private function checkSignature() //官方的验证函数
  15. {
  16. $signature = $_GET["signature"];
  17. $timestamp = $_GET["timestamp"];
  18. $nonce = $_GET["nonce"];
  19. $token = TOKEN;
  20. $tmpArr = array($token, $timestamp, $nonce);
  21. sort($tmpArr, SORT_STRING);
  22. $tmpStr = implode( $tmpArr );
  23. $tmpStr = sha1( $tmpStr );
  24. if( $tmpStr == $signature ){
  25. return true;
  26. }else{
  27. return false;
  28. }
  29. }
  30. };

2.2 设置小程序后台消息推送相关信息  URL(服务器地址):https://hosts.com/xx/test.php  Token: 任意符合规范的字符串,如上面定义的"xxxxx"  EncodingAESKey(消息加密密钥):(随机生成,自己保存就好了,这个加解密才用)  消息加密方式:暂时选择明文,不用去考虑加密解密的问题。  数据格式:根据需求选择。  提交,如果没有问题就会成功了。(如果有问题你可以用假的数据通过浏览器测试)



易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/23468.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询