> php教程 > php手册 > WeChat 공개 계정 개발을 위한 문자 메시지 자동 회신

WeChat 공개 계정 개발을 위한 문자 메시지 자동 회신

WBOY
풀어 주다: 2016-08-08 08:49:47
원래의
871명이 탐색했습니다.

1.PHP 샘플코드 다운로드

다운로드 주소 1: http://pan.baidu.com/s/1nvlhbnV,

다운로드 주소 2: https://mp.weixin.qq.com/wiki/home/index.html (개발 시작 - "액세스 가이드 -" PHP 샘플 코드 다운로드)

2.wx_sample.php 초기코드

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">  * wechat php test
</span><span style="color: #008080;"> 4</span>   <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">define your token</span>
<span style="color: #008080;"> 7</span> <span style="color: #008080;">define</span>("TOKEN", "weixin"<span style="color: #000000;">);
</span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$wechatObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> wechatCallbackapiTest();
</span><span style="color: #008080;"> 9</span> <span style="color: #800080;">$wechatObj</span>-><span style="color: #000000;">valid();
</span><span style="color: #008080;">10</span> 
<span style="color: #008080;">11</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> wechatCallbackapiTest
</span><span style="color: #008080;">12</span> <span style="color: #000000;">{
</span><span style="color: #008080;">13</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> valid()
</span><span style="color: #008080;">14</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">15</span>         <span style="color: #800080;">$echoStr</span> = <span style="color: #800080;">$_GET</span>["echostr"<span style="color: #000000;">];
</span><span style="color: #008080;">16</span> 
<span style="color: #008080;">17</span>         <span style="color: #008000;">//</span><span style="color: #008000;">valid signature , option</span>
<span style="color: #008080;">18</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">checkSignature()){
</span><span style="color: #008080;">19</span>             <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$echoStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">20</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">21</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">22</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">23</span> 
<span style="color: #008080;">24</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> responseMsg()
</span><span style="color: #008080;">25</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">26</span>         <span style="color: #008000;">//</span><span style="color: #008000;">get post data, May be due to the different environments</span>
<span style="color: #008080;">27</span>         <span style="color: #800080;">$postStr</span> = <span style="color: #800080;">$GLOBALS</span>["HTTP_RAW_POST_DATA"<span style="color: #000000;">];
</span><span style="color: #008080;">28</span> 
<span style="color: #008080;">29</span>           <span style="color: #008000;">//</span><span style="color: #008000;">extract post data</span>
<span style="color: #008080;">30</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$postStr</span><span style="color: #000000;">)){
</span><span style="color: #008080;">31</span>                 <span style="color: #008000;">/*</span><span style="color: #008000;"> libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
</span><span style="color: #008080;">32</span> <span style="color: #008000;">                   the best way is to check the validity of xml by yourself </span><span style="color: #008000;">*/</span>
<span style="color: #008080;">33</span>                 libxml_disable_entity_loader(<span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #008080;">34</span>                   <span style="color: #800080;">$postObj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$postStr</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
</span><span style="color: #008080;">35</span>                 <span style="color: #800080;">$fromUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">FromUserName;
</span><span style="color: #008080;">36</span>                 <span style="color: #800080;">$toUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">ToUserName;
</span><span style="color: #008080;">37</span>                 <span style="color: #800080;">$keyword</span> = <span style="color: #008080;">trim</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Content);
</span><span style="color: #008080;">38</span>                 <span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();
</span><span style="color: #008080;">39</span>                 <span style="color: #800080;">$textTpl</span> = "<span style="color: #000000;"><xml>
</span><span style="color: #008080;">40</span> <span style="color: #000000;">                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span style="color: #008080;">41</span> <span style="color: #000000;">                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span style="color: #008080;">42</span> <span style="color: #000000;">                            <CreateTime>%s</CreateTime>
</span><span style="color: #008080;">43</span> <span style="color: #000000;">                            <MsgType><![CDATA[%s]]></MsgType>
</span><span style="color: #008080;">44</span> <span style="color: #000000;">                            <Content><![CDATA[%s]]></Content>
</span><span style="color: #008080;">45</span> <span style="color: #000000;">                            <FuncFlag>0</FuncFlag>
</span><span style="color: #008080;">46</span>                             </xml>"<span style="color: #000000;">;             
</span><span style="color: #008080;">47</span>                 <span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>( <span style="color: #800080;">$keyword</span><span style="color: #000000;"> ))
</span><span style="color: #008080;">48</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">49</span>                       <span style="color: #800080;">$msgType</span> = "text"<span style="color: #000000;">;
</span><span style="color: #008080;">50</span>                     <span style="color: #800080;">$contentStr</span> = "Welcome to wechat world!"<span style="color: #000000;">;
</span><span style="color: #008080;">51</span>                     <span style="color: #800080;">$resultStr</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$textTpl</span>, <span style="color: #800080;">$fromUsername</span>, <span style="color: #800080;">$toUsername</span>, <span style="color: #800080;">$time</span>, <span style="color: #800080;">$msgType</span>, <span style="color: #800080;">$contentStr</span><span style="color: #000000;">);
</span><span style="color: #008080;">52</span>                     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$resultStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">53</span>                 }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">54</span>                     <span style="color: #0000ff;">echo</span> "Input something..."<span style="color: #000000;">;
</span><span style="color: #008080;">55</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">56</span> 
<span style="color: #008080;">57</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;">58</span>             <span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">;
</span><span style="color: #008080;">59</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">60</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">61</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">62</span>         
<span style="color: #008080;">63</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkSignature()
</span><span style="color: #008080;">64</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">65</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> you must define TOKEN by yourself</span>
<span style="color: #008080;">66</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">defined</span>("TOKEN"<span style="color: #000000;">)) {
</span><span style="color: #008080;">67</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>('TOKEN is not defined!'<span style="color: #000000;">);
</span><span style="color: #008080;">68</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">69</span>         
<span style="color: #008080;">70</span>         <span style="color: #800080;">$signature</span> = <span style="color: #800080;">$_GET</span>["signature"<span style="color: #000000;">];
</span><span style="color: #008080;">71</span>         <span style="color: #800080;">$timestamp</span> = <span style="color: #800080;">$_GET</span>["timestamp"<span style="color: #000000;">];
</span><span style="color: #008080;">72</span>         <span style="color: #800080;">$nonce</span> = <span style="color: #800080;">$_GET</span>["nonce"<span style="color: #000000;">];
</span><span style="color: #008080;">73</span>                 
<span style="color: #008080;">74</span>         <span style="color: #800080;">$token</span> =<span style="color: #000000;"> TOKEN;
</span><span style="color: #008080;">75</span>         <span style="color: #800080;">$tmpArr</span> = <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$token</span>, <span style="color: #800080;">$timestamp</span>, <span style="color: #800080;">$nonce</span><span style="color: #000000;">);
</span><span style="color: #008080;">76</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> use SORT_STRING rule</span>
<span style="color: #008080;">77</span>         <span style="color: #008080;">sort</span>(<span style="color: #800080;">$tmpArr</span>,<span style="color: #000000;"> SORT_STRING);
</span><span style="color: #008080;">78</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">implode</span>( <span style="color: #800080;">$tmpArr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">79</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">sha1</span>( <span style="color: #800080;">$tmpStr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">80</span>         
<span style="color: #008080;">81</span>         <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$tmpStr</span> == <span style="color: #800080;">$signature</span><span style="color: #000000;"> ){
</span><span style="color: #008080;">82</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">83</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">84</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">85</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">86</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">87</span> <span style="color: #000000;">}
</span><span style="color: #008080;">88</span> 
<span style="color: #008080;">89</span> ?>
로그인 후 복사
wx_sample.php

3. 응답 메시지 메소드를 호출합니다

wx_sample.php 파일에서 $wechatObj->valid();를 주석 처리하고 그 아래에 "$wechatObj->responseMsg();"를 추가합니다.

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">  * wechat php test
</span><span style="color: #008080;"> 4</span>   <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">define your token</span>
<span style="color: #008080;"> 7</span> <span style="color: #008080;">define</span>("TOKEN", "weixin"<span style="color: #000000;">);
</span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$wechatObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> wechatCallbackapiTest();
</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">$wechatObj->valid();//接口验证
<span style="color: #008080;">10</span> <span style="color: #800080;">$wechatObj</span>->responseMsg();<span style="color: #008000;">//</span><span style="color: #008000;">调用回复消息方法</span>
<span style="color: #008080;">11</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> wechatCallbackapiTest
</span><span style="color: #008080;">12</span> <span style="color: #000000;">{
</span><span style="color: #008080;">13</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> valid()
</span><span style="color: #008080;">14</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">15</span>         <span style="color: #800080;">$echoStr</span> = <span style="color: #800080;">$_GET</span>["echostr"<span style="color: #000000;">];
</span><span style="color: #008080;">16</span> 
<span style="color: #008080;">17</span>         <span style="color: #008000;">//</span><span style="color: #008000;">valid signature , option</span>
<span style="color: #008080;">18</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">checkSignature()){
</span><span style="color: #008080;">19</span>             <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$echoStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">20</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">21</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">22</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">23</span> 
<span style="color: #008080;">24</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> responseMsg()
</span><span style="color: #008080;">25</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">26</span>         <span style="color: #008000;">//</span><span style="color: #008000;">get post data, May be due to the different environments</span>
<span style="color: #008080;">27</span>         <span style="color: #800080;">$postStr</span> = <span style="color: #800080;">$GLOBALS</span>["HTTP_RAW_POST_DATA"<span style="color: #000000;">];
</span><span style="color: #008080;">28</span> 
<span style="color: #008080;">29</span>           <span style="color: #008000;">//</span><span style="color: #008000;">extract post data</span>
<span style="color: #008080;">30</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$postStr</span><span style="color: #000000;">)){
</span><span style="color: #008080;">31</span>                 <span style="color: #008000;">/*</span><span style="color: #008000;"> libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
</span><span style="color: #008080;">32</span> <span style="color: #008000;">                   the best way is to check the validity of xml by yourself </span><span style="color: #008000;">*/</span>
<span style="color: #008080;">33</span>                 libxml_disable_entity_loader(<span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #008080;">34</span>                   <span style="color: #800080;">$postObj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$postStr</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
</span><span style="color: #008080;">35</span>                 <span style="color: #800080;">$fromUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">FromUserName;
</span><span style="color: #008080;">36</span>                 <span style="color: #800080;">$toUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">ToUserName;
</span><span style="color: #008080;">37</span>                 <span style="color: #800080;">$keyword</span> = <span style="color: #008080;">trim</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Content);
</span><span style="color: #008080;">38</span>                 <span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();
</span><span style="color: #008080;">39</span>                 <span style="color: #800080;">$textTpl</span> = "<span style="color: #000000;"><xml>
</span><span style="color: #008080;">40</span> <span style="color: #000000;">                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span style="color: #008080;">41</span> <span style="color: #000000;">                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span style="color: #008080;">42</span> <span style="color: #000000;">                            <CreateTime>%s</CreateTime>
</span><span style="color: #008080;">43</span> <span style="color: #000000;">                            <MsgType><![CDATA[%s]]></MsgType>
</span><span style="color: #008080;">44</span> <span style="color: #000000;">                            <Content><![CDATA[%s]]></Content>
</span><span style="color: #008080;">45</span> <span style="color: #000000;">                            <FuncFlag>0</FuncFlag>
</span><span style="color: #008080;">46</span>                             </xml>"<span style="color: #000000;">;             
</span><span style="color: #008080;">47</span>                 <span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>( <span style="color: #800080;">$keyword</span><span style="color: #000000;"> ))
</span><span style="color: #008080;">48</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">49</span>                       <span style="color: #800080;">$msgType</span> = "text"<span style="color: #000000;">;
</span><span style="color: #008080;">50</span>                     <span style="color: #800080;">$contentStr</span> = "Welcome to wechat world!"<span style="color: #000000;">;
</span><span style="color: #008080;">51</span>                     <span style="color: #800080;">$resultStr</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$textTpl</span>, <span style="color: #800080;">$fromUsername</span>, <span style="color: #800080;">$toUsername</span>, <span style="color: #800080;">$time</span>, <span style="color: #800080;">$msgType</span>, <span style="color: #800080;">$contentStr</span><span style="color: #000000;">);
</span><span style="color: #008080;">52</span>                     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$resultStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">53</span>                 }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">54</span>                     <span style="color: #0000ff;">echo</span> "Input something..."<span style="color: #000000;">;
</span><span style="color: #008080;">55</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">56</span> 
<span style="color: #008080;">57</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;">58</span>             <span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">;
</span><span style="color: #008080;">59</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">60</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">61</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">62</span>         
<span style="color: #008080;">63</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkSignature()
</span><span style="color: #008080;">64</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">65</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> you must define TOKEN by yourself</span>
<span style="color: #008080;">66</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">defined</span>("TOKEN"<span style="color: #000000;">)) {
</span><span style="color: #008080;">67</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>('TOKEN is not defined!'<span style="color: #000000;">);
</span><span style="color: #008080;">68</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">69</span>         
<span style="color: #008080;">70</span>         <span style="color: #800080;">$signature</span> = <span style="color: #800080;">$_GET</span>["signature"<span style="color: #000000;">];
</span><span style="color: #008080;">71</span>         <span style="color: #800080;">$timestamp</span> = <span style="color: #800080;">$_GET</span>["timestamp"<span style="color: #000000;">];
</span><span style="color: #008080;">72</span>         <span style="color: #800080;">$nonce</span> = <span style="color: #800080;">$_GET</span>["nonce"<span style="color: #000000;">];
</span><span style="color: #008080;">73</span>                 
<span style="color: #008080;">74</span>         <span style="color: #800080;">$token</span> =<span style="color: #000000;"> TOKEN;
</span><span style="color: #008080;">75</span>         <span style="color: #800080;">$tmpArr</span> = <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$token</span>, <span style="color: #800080;">$timestamp</span>, <span style="color: #800080;">$nonce</span><span style="color: #000000;">);
</span><span style="color: #008080;">76</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> use SORT_STRING rule</span>
<span style="color: #008080;">77</span>         <span style="color: #008080;">sort</span>(<span style="color: #800080;">$tmpArr</span>,<span style="color: #000000;"> SORT_STRING);
</span><span style="color: #008080;">78</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">implode</span>( <span style="color: #800080;">$tmpArr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">79</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">sha1</span>( <span style="color: #800080;">$tmpStr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">80</span>         
<span style="color: #008080;">81</span>         <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$tmpStr</span> == <span style="color: #800080;">$signature</span><span style="color: #000000;"> ){
</span><span style="color: #008080;">82</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">83</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">84</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">85</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">86</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">87</span> <span style="color: #000000;">}
</span><span style="color: #008080;">88</span> 
<span style="color: #008080;">89</span> ?>
로그인 후 복사
전화 답장 메시지 방법

4. 키워드 자동 답글 및 팔로우 답글

$keyword는 사용자가 WeChat에서 보낸 문자 메시지를 저장합니다.

공식 개발자 문서: https://mp.weixin.qq.com/wiki/home/index.html (메시지 관리-"메시지 수신-이벤트 푸시 수신-"1. 이벤트 팔로우/언팔로우)

관심 이벤트와 일반 문자 메시지에는 두 가지 차이점이 있습니다. 하나는 MsgType 값이 event이고, 다른 하나는 추가된 Event 값이 subscribe라는 점입니다. 공식 문서(원본 wx_sample.php)에서는 이 매개변수를 추출하지 않으므로 직접 추출해야 합니다. 두 개의 변수 $msgType 및 $event를 프로그램에 추가합니다.

<span style="color: #008080;">  1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;">  2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">  3</span> <span style="color: #008000;">  * wechat php test
</span><span style="color: #008080;">  4</span>   <span style="color: #008000;">*/</span>
<span style="color: #008080;">  5</span> 
<span style="color: #008080;">  6</span> <span style="color: #008000;">//</span><span style="color: #008000;">define your token</span>
<span style="color: #008080;">  7</span> <span style="color: #008080;">define</span>("TOKEN", "weixin"<span style="color: #000000;">);
</span><span style="color: #008080;">  8</span> <span style="color: #800080;">$wechatObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> wechatCallbackapiTest();
</span><span style="color: #008080;">  9</span> <span style="color: #008000;">//</span><span style="color: #008000;">$wechatObj->valid();//接口验证
<span style="color: #008080;"> 10</span> <span style="color: #800080;">$wechatObj</span>->responseMsg();<span style="color: #008000;">//</span><span style="color: #008000;">调用回复消息方法</span>
<span style="color: #008080;"> 11</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> wechatCallbackapiTest
</span><span style="color: #008080;"> 12</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 13</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> valid()
</span><span style="color: #008080;"> 14</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 15</span>         <span style="color: #800080;">$echoStr</span> = <span style="color: #800080;">$_GET</span>["echostr"<span style="color: #000000;">];
</span><span style="color: #008080;"> 16</span> 
<span style="color: #008080;"> 17</span>         <span style="color: #008000;">//</span><span style="color: #008000;">valid signature , option</span>
<span style="color: #008080;"> 18</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">checkSignature()){
</span><span style="color: #008080;"> 19</span>             <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$echoStr</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 20</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 21</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 22</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 23</span> 
<span style="color: #008080;"> 24</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> responseMsg()
</span><span style="color: #008080;"> 25</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 26</span>         <span style="color: #008000;">//</span><span style="color: #008000;">get post data, May be due to the different environments</span>
<span style="color: #008080;"> 27</span>         <span style="color: #800080;">$postStr</span> = <span style="color: #800080;">$GLOBALS</span>["HTTP_RAW_POST_DATA"<span style="color: #000000;">];
</span><span style="color: #008080;"> 28</span> 
<span style="color: #008080;"> 29</span>           <span style="color: #008000;">//</span><span style="color: #008000;">extract post data</span>
<span style="color: #008080;"> 30</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$postStr</span><span style="color: #000000;">)){
</span><span style="color: #008080;"> 31</span>                 <span style="color: #008000;">/*</span><span style="color: #008000;"> libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
</span><span style="color: #008080;"> 32</span> <span style="color: #008000;">                   the best way is to check the validity of xml by yourself </span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 33</span>                 libxml_disable_entity_loader(<span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 34</span>                   <span style="color: #800080;">$postObj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$postStr</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
</span><span style="color: #008080;"> 35</span>                 <span style="color: #800080;">$fromUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">FromUserName;
</span><span style="color: #008080;"> 36</span>                 <span style="color: #800080;">$toUsername</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">ToUserName;
</span><span style="color: #008080;"> 37</span>                 <span style="color: #800080;">$keyword</span> = <span style="color: #008080;">trim</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Content);
</span><span style="color: #008080;"> 38</span>                 <span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();
</span><span style="color: #008080;"> 39</span>                 <span style="color: #800080;">$msgType</span> = <span style="color: #800080;">$postObj</span>->MsgType;<span style="color: #008000;">//</span><span style="color: #008000;">消息类型</span>
<span style="color: #008080;"> 40</span>                 <span style="color: #800080;">$event</span> = <span style="color: #800080;">$postObj</span>->Event;<span style="color: #008000;">//</span><span style="color: #008000;">时间类型,subscribe(订阅)、unsubscribe(取消订阅)</span>
<span style="color: #008080;"> 41</span>                 <span style="color: #800080;">$textTpl</span> = "<span style="color: #000000;"><xml>
</span><span style="color: #008080;"> 42</span> <span style="color: #000000;">                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span style="color: #008080;"> 43</span> <span style="color: #000000;">                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span style="color: #008080;"> 44</span> <span style="color: #000000;">                            <CreateTime>%s</CreateTime>
</span><span style="color: #008080;"> 45</span> <span style="color: #000000;">                            <MsgType><![CDATA[%s]]></MsgType>
</span><span style="color: #008080;"> 46</span> <span style="color: #000000;">                            <Content><![CDATA[%s]]></Content>
</span><span style="color: #008080;"> 47</span> <span style="color: #000000;">                            <FuncFlag>0</FuncFlag>
</span><span style="color: #008080;"> 48</span>                             </xml>"<span style="color: #000000;">; 
</span><span style="color: #008080;"> 49</span>                            
<span style="color: #008080;"> 50</span>                 <span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$msgType</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 51</span>                     <span style="color: #0000ff;">case</span> "event":
<span style="color: #008080;"> 52</span>                     <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$event</span>=="subscribe"<span style="color: #000000;">){
</span><span style="color: #008080;"> 53</span>                         <span style="color: #800080;">$contentStr</span> = "Hi,欢迎关注海仙日用百货!"."\n"."回复数字'1',了解店铺地址."."\n"."回复数字'2',了解商品种类."<span style="color: #000000;">;
</span><span style="color: #008080;"> 54</span> <span style="color: #000000;">                    } 
</span><span style="color: #008080;"> 55</span>                     <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 56</span>                     <span style="color: #0000ff;">case</span> "text":
<span style="color: #008080;"> 57</span>                         <span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$keyword</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 58</span>                             <span style="color: #0000ff;">case</span> "1":
<span style="color: #008080;"> 59</span>                             <span style="color: #800080;">$contentStr</span> = "店铺地址:"."\n"."杭州市江干艮山西路233号新东升市场地下室第一排."<span style="color: #000000;">;    
</span><span style="color: #008080;"> 60</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 61</span>                             <span style="color: #0000ff;">case</span> "2":
<span style="color: #008080;"> 62</span>                             <span style="color: #800080;">$contentStr</span> = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"
<span style="color: #008080;"> 63</span>                                          ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等."<span style="color: #000000;">;
</span><span style="color: #008080;"> 64</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 65</span>                             <span style="color: #0000ff;">default</span>:
<span style="color: #008080;"> 66</span>                             <span style="color: #800080;">$contentStr</span> = "对不起,你的内容我会稍后回复"<span style="color: #000000;">;
</span><span style="color: #008080;"> 67</span> <span style="color: #000000;">                        }
</span><span style="color: #008080;"> 68</span>                     <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 69</span> <span style="color: #000000;">                }
</span><span style="color: #008080;"> 70</span>                 <span style="color: #800080;">$msgType</span> = "text"<span style="color: #000000;">;
</span><span style="color: #008080;"> 71</span>                 <span style="color: #800080;">$resultStr</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$textTpl</span>, <span style="color: #800080;">$fromUsername</span>, <span style="color: #800080;">$toUsername</span>, <span style="color: #800080;">$time</span>, <span style="color: #800080;">$msgType</span>, <span style="color: #800080;">$contentStr</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 72</span>                 <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$resultStr</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 73</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;"> 74</span>             <span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">;
</span><span style="color: #008080;"> 75</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 77</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 78</span>         
<span style="color: #008080;"> 79</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkSignature()
</span><span style="color: #008080;"> 80</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 81</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> you must define TOKEN by yourself</span>
<span style="color: #008080;"> 82</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">defined</span>("TOKEN"<span style="color: #000000;">)) {
</span><span style="color: #008080;"> 83</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>('TOKEN is not defined!'<span style="color: #000000;">);
</span><span style="color: #008080;"> 84</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 85</span>         
<span style="color: #008080;"> 86</span>         <span style="color: #800080;">$signature</span> = <span style="color: #800080;">$_GET</span>["signature"<span style="color: #000000;">];
</span><span style="color: #008080;"> 87</span>         <span style="color: #800080;">$timestamp</span> = <span style="color: #800080;">$_GET</span>["timestamp"<span style="color: #000000;">];
</span><span style="color: #008080;"> 88</span>         <span style="color: #800080;">$nonce</span> = <span style="color: #800080;">$_GET</span>["nonce"<span style="color: #000000;">];
</span><span style="color: #008080;"> 89</span>                 
<span style="color: #008080;"> 90</span>         <span style="color: #800080;">$token</span> =<span style="color: #000000;"> TOKEN;
</span><span style="color: #008080;"> 91</span>         <span style="color: #800080;">$tmpArr</span> = <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$token</span>, <span style="color: #800080;">$timestamp</span>, <span style="color: #800080;">$nonce</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 92</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> use SORT_STRING rule</span>
<span style="color: #008080;"> 93</span>         <span style="color: #008080;">sort</span>(<span style="color: #800080;">$tmpArr</span>,<span style="color: #000000;"> SORT_STRING);
</span><span style="color: #008080;"> 94</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">implode</span>( <span style="color: #800080;">$tmpArr</span><span style="color: #000000;"> );
</span><span style="color: #008080;"> 95</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">sha1</span>( <span style="color: #800080;">$tmpStr</span><span style="color: #000000;"> );
</span><span style="color: #008080;"> 96</span>         
<span style="color: #008080;"> 97</span>         <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$tmpStr</span> == <span style="color: #800080;">$signature</span><span style="color: #000000;"> ){
</span><span style="color: #008080;"> 98</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 99</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">100</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">101</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">102</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">103</span> <span style="color: #000000;">}
</span><span style="color: #008080;">104</span> 
<span style="color: #008080;">105</span> 
<span style="color: #008080;">106</span> ?>
로그인 후 복사
키워드 답글 및 팔로우 답글

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿