Introduction to PHP WeChat development text automatic reply

Source: Internet
Author: User
This article mainly gives you a detailed introduction to PHP development-related materials for simple implementation of automatic text reply, interested friends can refer to this article's PHP Development text to automatically reply to the relevant information, interested friends can refer

First, register an account on the public platform (many items are required for registration). After registration, log in. You can see the "Developer Center" on the left. Before you start the developer center, it seems that you need to complete some information and follow the steps to complete it. Go to the developer center and edit

Header ('content-type: text/html; charset = utf-8 '); define ("TOKEN", "my_weixin "); // define your token $ wx = new wechatCallbackapiTest (); if ($ _ GET ['echostr']) {$ wx-> valid (); // If echostr is sent, perform verification} else {$ wx-> responseMsg (); // if no echostr exists, return the message} class wechatCallbackapiTest {public function valid () {// valid signature, option $ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {// call the verification field echo $ echoStr; exit; } Public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receive XML data // extract post data if (! Empty ($ postStr) {// Parse the XML from post as an object $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ fromUsername = $ postObj-> FromUserName; // The user requesting the message $ toUsername = $ postObj-> ToUserName; // "my" public id $ keyword = trim ($ postObj-> Content); // message Content $ time = time (); // timestamp $ msgtype = 'text'; // Message Type: text $ textTpl ="
   
  %s
    
  %s
    
  
   
% S
    
  %s
    
  %s
    
 "; If ($ keyword = 'hehes') {$ contentStr = 'Hello world !!! '; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();} else {$ contentStr = 'input hehe test'; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit () ;}} else {echo ""; exit ;}// verification field 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 ;}}}

If a message is sent, the system prompts that the public platform cannot provide a shared service temporarily. please try again later. Most of them are code syntax problems. check the syntax errors and try again.

Appendix:

When a new user pays attention to your public account, information is automatically returned: (add this code before determining $ keyword ).

If ($ postObj-> MsgType = 'event') {// if the message type in XML is event if ($ postObj-> event = 'subscribe ') {// if it is a subscription event $ contentStr = "Welcome to subscribe to misaka last summer! \ N more highlights: http://www.php.cn/"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}}

The above is a detailed description of the automatic reply to PHP Development text. For more information, see other related articles on php Chinese network!

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.