PHP WeChat development text automatic reply, _ PHP Tutorial

Source: Internet
Author: User
PHP Development text automatic reply ,. The PHP Development text is automatically replied. first, register an account on the public platform (there are many items to be filled in for registration). After registration, log in. The PHP Development text on the left is automatically replied,

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

Modify the configuration. Note the following when modifying the configuration:


The URL is the PHP script under your own domain name (the demo with this script is read down), which is used to connect to the interface. Like http://www.example.com/weixin.php

Token is a constant defined in the above script. for example, your PHP script defines:

Define ("TOKEN", "my_weixin ");

Then, when you fill in the Token, you fill in abcdefgh

EncodingAESKey is used for message encryption. You can write a 43-character combination of numbers and letters, or select "random generation". Generally, you can select random generation.

After filling in the information, save the Token. (if the token verification fails, make sure the Token is consistent and save it several times ).

After saving the configuration, click "enable" next to modifying the configuration ".

Then, you can edit your PHP script. (If you do not have your own domain name, you can use Sina Cloud's free SAE, and it is best to complete real-name authentication)
The demo script is as follows: follow this public platform (for example, after the subscription number). The function is: input hehe and return hello world !!! If you enter other characters, heeh is returned.

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://blog.csdn.net/misakaqunianxiatian "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Register. first, register an account on the public platform (many items are required for registration). After registration, log in. You can see the left...

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.