WeChat keyword auto-reply code

Source: Internet
Author: User
: This article mainly introduces the keyword auto-reply code. if you are interested in the PHP Tutorial, refer to it.
/** Wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new WechatCallbackapiTest (); $ wechatObj-> valid (); $ wechatObj-> responseMsg (); class WechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ['echostr']; // valid signature, optionif ($ this-> checkSignature () {echo $ echoStr; exit;} public function responseMsg () {// get post data, May be due to the different environments $ post Str = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post dataif (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ tpUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ textTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  %s
  
  %s
  
  
   
0
  
 "; If (! Empty ($ keyword) {$ msgType = "text"; $ contentStr = "1 phone mailbox 2. qq information 3 address information "; if ($ keyword =" 1 ") {// when the keyword is 1 reply $ contentStr =" Phone: 15057190640 Email: 2323178881@qq.com !! ";} If ($ keyword =" 2 ") {// when the keyword is 2, reply $ contentStr =" My QQ is 2323178881! ";} If ($ keyword =" 3 ") {// when the keyword is 3, $ contentStr =" Hangzhou Jianggan district, Zhejiang province! ";}$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ ToUserName, $ time, $ msgType, $ contentStr); echo $ resultStr ;} else {echo "Input something... ";}} else {echo" "; exit ;}} private function checkSignature () {$ signature =$ _ GET [" signature "]; $ timestamp = $ _ GET ['timestamp']; $ nonce = $ _ GET ['nonce ']; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr); $ tmpStr = implode ('', $ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr = $ signature) {return true;} else {return false ;}}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the keyword auto-reply code, including the content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.