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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * wechat php test
  4. */
  5. //define your token
  6. define("TOKEN", "xiaomo");
  7. $wechatObj = new wechatCallbackapiTest();
  8. if (isset($_GET['echostr'])) {
  9. $wechatObj->valid();
  10. }
  11. $wechatObj->responseMsg();
  12. class wechatCallbackapiTest
  13. {
  14. public function valid()
  15. {
  16. $echoStr = $_GET["echostr"];
  17. //valid signature , option
  18. if($this->checkSignature()){
  19. echo $echoStr;
  20. exit;
  21. }
  22. }
  23. public function responseMsg()
  24. {
  25. //get post data, May be due to the different environments
  26. //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  27. $postStr = file_get_contents("php://input");
  28. file_put_contents("./demo.txt",date("Y-m-d H:i:s")." ".json_encode($postStr).PHP_EOL,FILE_APPEND);
  29. //extract post data
  30. if (!empty($postStr)){
  31. /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
  32. the best way is to check the validity of xml by yourself */
  33. libxml_disable_entity_loader(true);
  34. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  35. $fromUsername = $postObj->FromUserName;
  36. $toUsername = $postObj->ToUserName;
  37. $keyword = trim($postObj->Content);
  38. $type=$postObj->MsgType;
  39. $event="";
  40. if ($type =='event'){
  41. $event=$postObj->Event;
  42. }
  43. $time = time();
  44. $textTpl = "<xml>
  45. <ToUserName><![CDATA[%s]]></ToUserName>
  46. <FromUserName><![CDATA[%s]]></FromUserName>
  47. <CreateTime>%s</CreateTime>
  48. <MsgType><![CDATA[%s]]></MsgType>
  49. <Content><![CDATA[%s]]></Content>
  50. </xml>";
  51. if(!empty( $keyword ) || $event =="subscribe")
  52. {
  53. $msgType = "text";
  54. if ($event == "subscribe"){
  55. $contentStr = "欢迎关注,此号可以自由聊天^_^";
  56. }else{
  57. if (strpos($keyword,"主人") !== false){
  58. $contentStr="我的主人是宇宙无敌超级大帅哥--人称陌帅";
  59. }else{
  60. $contentStr = $this->simsimiHttp($keyword);
  61. }
  62. }
  63. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  64. echo $resultStr;
  65. }else{
  66. echo "Input something...";
  67. }
  68. }else {
  69. echo "";
  70. exit;
  71. }
  72. }
  73. private function checkSignature()
  74. {
  75. // you must define TOKEN by yourself
  76. if (!defined("TOKEN")) {
  77. throw new Exception('TOKEN is not defined!');
  78. }
  79. $signature = $_GET["signature"];
  80. $timestamp = $_GET["timestamp"];
  81. $nonce = $_GET["nonce"];
  82. $token = TOKEN;
  83. $tmpArr = array($token, $timestamp, $nonce);
  84. // use SORT_STRING rule
  85. sort($tmpArr, SORT_STRING);
  86. $tmpStr = implode( $tmpArr );
  87. $tmpStr = sha1( $tmpStr );
  88. if( $tmpStr == $signature ){
  89. return true;
  90. }else{
  91. return false;
  92. }
  93. }
  94. private function simsimiHttp($msg){
  95. $url="http://www.xiaodoubi.com/simsimiapi.php?msg=".$msg;
  96. $res = file_get_contents($url);
  97. file_put_contents("./demo.txt",date("Y-m-d H:i:s")." ".$res.PHP_EOL,FILE_APPEND);
  98. if (strpos($res,"xiaodouqqcom") !== false){
  99. $res="无法回答,请不要发一下奇怪的问题或字符😒";
  100. }
  101. return $res;
  102. }
  103. }
  104. ?>