Home  >  Article  >  Backend Development  >  Tutorial 2: Reply to a simple text message, reply to a text message_PHP Tutorial

Tutorial 2: Reply to a simple text message, reply to a text message_PHP Tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:28974browse

Tutorial 2: Reply to a simple text message, reply to a text message

Previously we completed the `server configuration` of the WeChat official account and completed the token Verification.
Now we try to reply to the text message sent by the user to the official account.

1. First we make sure that the `WeChat configuration` is in the `enabled` state.

                     🎜>
2. Then we enter the `sandbox test account` of the WeChat public platform, because in this state the permission restrictions on various excuses are relatively small, and this is An independent official account, with independent `appID` `appsecret` `url` `token`, does not affect the online environment!
This is an independent official account, with independent `appID` `appsecret` `url ` `token`, does not affect the online environment!
This is an independent public account, with independent `appID` `appsecret` `url` `token`, does not affect the online environment!
(Important Say things three times)
                                                                                                                                                                                                                                                                               After completion, the test account configuration information will appear.
Modify the `URL` and `Token` according to your needs I still use the URL and Token of our SAE before, which are:
URL: http:/ /weixinshow001.sinaapp.comToken:weixin

                                                                                                         
Because the previous function only provided token verification,
did not implement any message reply function.
Now we use a wechat php library that has been written to implement text message reply.
The valid codes are as follows:





The processing flow of the code is,
step 1: Create an instance of TestWechat and write various configuration data.
step 2: When the user sends information to the official account, read The text content sent by the user is replied to the user as a text message.

<?<span>php
</span><span>/*</span><span>*
* 微信公众平台 PHP SDK 示例文件
</span><span>*/</span>
  <span>//</span><span>导入wechat的php类库</span>
  <span>require</span>('wechat/Wechat.php'<span>);
  </span><span>/*</span><span>*
   * 微信公众平台演示类
   </span><span>*/</span>
  <span>//</span><span>继承Wechat类,这里实现了消息回复和接受的基本功能</span>
  <span>class</span> TestWechat <span>extends</span><span> Wechat {
    </span><span>/*</span><span>*
     * 收到文本消息时触发,回复收到的文本消息内容
     *
     * @return void
     </span><span>*/</span>
    <span>//</span><span>step 2
    // 收到用户发给公众号的文本信息的时候会调用此函数</span>
    <span>protected</span> <span>function</span><span> onText() {
      </span><span>//</span><span>$this->getRequest('content') 返回的是用户发送给公众号的文本</span>
      <span>$this</span>->responseText('收到了文字消息:' . <span>$this</span>->getRequest('content'<span>));
    }
  }
  </span><span>//</span><span>step 1
  //新建一个Wechat的实例,用于处理用户发送过来的信息
  //aeskey 可以随便写,测试账号没有使用消息加密
  //token以及appid与`测试号管理`页的信息相同,不是公众号的信息
  //token以及appid与`测试号管理`页的信息相同,不是公众号的信息
  //token以及appid与`测试号管理`页的信息相同,不是公众号的信息</span>
<span>  (重要的事情说三遍)
  </span><span>$wechat</span> = <span>new</span> TestWechat(<span>array</span><span>(
    </span>'token' => 'weixin',
    'aeskey' => 'xxx',
    'appid' => 'wx5d1fb434a1652ae8',
    'debug' => <span>true</span><span>
    ));
  </span><span>$wechat</span>-><span>run();
复制代码</span>
The final code structure is: The communication between the Zhonghe test account is as follows:

                                                                                                                                          For a detailed introduction.



Thank you for reading, please understand with your heart! Hope this helps you as a beginner! ! Sharing is also a joy! ! ! Please pass on. . .


http://www.bkjia.com/PHPjc/1130145.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/1130145.html
TechArticle
Tutorial 2: Reply to a simple text message, reply to a text message Previously we completed the `server configuration` of the WeChat official account , and the verification of the token is completed. Now we try to reply to the user's message...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn