You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
5.2 KiB

7 years ago
  1. <?php
  2. /**
  3. * wechat php test
  4. */
  5. //define your token
  6. define("TOKEN", "weixin");
  7. $wechatObj = new wechatCallbackapiTest();//将11行的class类实例化
  8. $wechatObj->valid();//使用-》访问类中valid方法,用来验证开发模式
  9. //11--23行代码为签名及接口验证。
  10. class wechatCallbackapiTest
  11. {
  12. public function valid()//验证接口的方法
  13. {
  14. $echoStr = $_GET["echostr"];//从微信用户端获取一个随机字符赋予变量echostr
  15. //valid signature , option访问地61行的checkSignature签名验证方法,如果签名一致,输出变量echostr,完整验证配置接口的操作
  16. if($this->checkSignature()){
  17. echo $echoStr;
  18. exit;
  19. }
  20. }
  21. //公有的responseMsg的方法,是我们回复微信的关键。以后的章节修改代码就是修改这个。
  22. public function responseMsg()
  23. {
  24. //get post data, May be due to the different environments
  25. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//将用户端放松的数据保存到变量postStr中,由于微信端发送的都是xml,使用postStr无法解析,故使用$GLOBALS["HTTP_RAW_POST_DATA"]获取
  26. //extract post data如果用户端数据不为空,执行30-55否则56-58
  27. if (!empty($postStr)){
  28. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);//将postStr变量进行解析并赋予变量postObj。simplexml_load_string()函数是php中一个解析XML的函数,SimpleXMLElement为新对象的类,LIBXML_NOCDATA表示将CDATA设置为文本节点,CDATA标签中的文本XML不进行解析
  29. $fromUsername = $postObj->FromUserName;//将微信用户端的用户名赋予变量FromUserName
  30. $toUsername = $postObj->ToUserName;//将你的微信公众账号ID赋予变量ToUserName
  31. $keyword = trim($postObj->Content);//将用户微信发来的文本内容去掉空格后赋予变量keyword
  32. $time = time();//将系统时间赋予变量time
  33. //构建XML格式的文本赋予变量textTpl,注意XML格式为微信内容固定格式,详见文档
  34. $textTpl = "<xml>
  35. <ToUserName><![CDATA[%s]]></ToUserName>
  36. <FromUserName><![CDATA[%s]]></FromUserName>
  37. <CreateTime>%s</CreateTime>
  38. <MsgType><![CDATA[%s]]></MsgType>
  39. <Content><![CDATA[%s]]></Content>
  40. <FuncFlag>0</FuncFlag>
  41. </xml>";
  42. //39行,%s表示要转换成字符的数据类型,CDATA表示不转义
  43. //40行为微信来源方
  44. //41行为系统时间
  45. //42行为回复微信的信息类型
  46. //43行为回复微信的内容
  47. //44行为是否星标微信
  48. //XML格式文本结束符号
  49. if(!empty( $keyword ))//如果用户端微信发来的文本内容不为空,执行46--51否则52--53
  50. {
  51. $msgType = "text";//回复文本信息类型为text型,变量类型为msgType
  52. $contentStr = "Welcome to wechat world!";//我们进行文本输入的内容,变量名为contentStr,如果你要更改回复信息,就在这儿
  53. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);//将XML格式中的变量分别赋值。注意sprintf函数
  54. echo $resultStr;//输出回复信息,即发送微信
  55. }else{
  56. echo "Input something...";//不发送到微信端,只是测试使用
  57. }
  58. }else {
  59. echo "";//回复为空,无意义,调试用
  60. exit;
  61. }
  62. }
  63. //签名验证程序 ,checkSignature被18行调用。官方加密、校验流程:将token,timestamp,nonce这三个参数进行字典序排序,然后将这三个参数字符串拼接成一个字符串惊喜shal加密,开发者获得加密后的字符串可以与signature对比,表示该请求来源于微信。
  64. private function checkSignature()
  65. {
  66. $signature = $_GET["signature"];//从用户端获取签名赋予变量signature
  67. $timestamp = $_GET["timestamp"];//从用户端获取时间戳赋予变量timestamp
  68. $nonce = $_GET["nonce"]; //从用户端获取随机数赋予变量nonce
  69. $token = TOKEN;//将常量token赋予变量token
  70. $tmpArr = array($token, $timestamp, $nonce);//简历数组变量tmpArr
  71. sort($tmpArr, SORT_STRING);//新建排序
  72. $tmpStr = implode( $tmpArr );//字典排序
  73. $tmpStr = sha1( $tmpStr );//shal加密
  74. //tmpStr与signature值相同,返回真,否则返回假
  75. if( $tmpStr == $signature ){
  76. return true;
  77. }else{
  78. return false;
  79. }
  80. }
  81. }
  82. ?>