Table of Contents
WeChat public platform message body encryption and decryption implementation, public decryption
1. Message body encryption and decryption
1. Configuration
2. Encryption and decryption implementation
3. Complete code
How to send messages on WeChat public platform
Home Backend Development PHP Tutorial Implementation of message body encryption and decryption on WeChat public platform, public decryption_PHP tutorial

Implementation of message body encryption and decryption on WeChat public platform, public decryption_PHP tutorial

Jul 13, 2016 am 10:15 AM
Keywords accomplish platform WeChat information sign Decrypt

WeChat public platform message body encryption and decryption implementation, public decryption

Keywords: WeChat public platform message body signature message body encryption and decryption EncodingAESKey security mode

Original text http://www.cnblogs.com/txw1958/p/weixin-aes-encrypt-Decrypt.html

1. Message body encryption and decryption

When configuring the server, the WeChat public platform provides three encryption and decryption modes for developers to choose, namely plaintext mode, compatibility mode, and security mode. Before selecting compatibility mode and security mode, you need to fill in the message addition in the developer center Decryption key EncodingAESKey.

  • Clear text mode: Maintain the existing mode, no new encryption and decryption features are adapted, message body is sent and received in plain text, the default setting is plain text mode
  • Compatibility mode: The content of messages sent by the public platform will include both plain text and cipher text, and the length of the message packet will be increased to about 3 times the original; the public account can reply in plain text or cipher text, without affecting the existing message sending and receiving; developers can Debugging in this mode
  • Safe mode (recommended): The content of the message body sent by the public platform only contains ciphertext, and the message body replied by the public account is also ciphertext. It is recommended that developers use this mode to send and receive messages after successful debugging

What is EncodingAESKey?

  • The WeChat public platform uses the AES symmetric encryption algorithm to encrypt the message body pushed to the public account. EncodingAESKey is the secret key used for encryption. The public account uses this secret key to decrypt the received ciphertext message body, and the reply message body is also encrypted with this secret key. For the principle of AES symmetric encryption algorithm, please refer to http://www.cnblogs.com/txw1958/p/aes.html

For detailed technical solutions for encryption and decryption, please refer to the official document http://mp.weixin.qq.com/wiki/index.php?title=%E6%8A%80%E6%9C%AF%E6%96% B9%E6%A1%88

Applicable public account types

  • Verified subscription number
  • Service ID
  • Enterprise ID

cannot be used for unauthenticated subscription accounts because it does not have the appid parameter


2. Development, implementation and data analysis

1. Configuration

Assume that the URL in this development configuration is

http:<span>//</span><span>www.fangbei.org/index.php</span>
Copy after login

The following three parameters need to be configured in the interface program

<span>/*</span><span>
    方倍工作室 http://www.cnblogs.com/txw1958/
    CopyRight 2014 All Rights Reserved
</span><span>*/</span>
<span>define</span>("TOKEN", "weixin"<span>);
</span><span>define</span>("AppID", "wxbad0b45542aa0b5e"<span>);
</span><span>define</span>("EncodingAESKey", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"<span>);
</span><span>require_once</span>('wxBizMsgCrypt.php');
Copy after login

2. Encryption and decryption implementation

When a user sends a message to a public account, the WeChat public account will bring signature, timestamp, nonce, encrypt_type, msg_signature and other parameters in the URL, as shown below

http:<span>//</span><span>www.fangbei.org/aes/index.php?signature=35703636de2f9df2a77a662b68e521ce17c34db4&timestamp=1414243737&nonce=1792106704&encrypt_type=aes&msg_signature=6147984331daf7a1a9eed6e0ec3ba69055256154</span>
Copy after login

At the same time, push the following XML message to the interface, which is an encrypted message

<span><</span><span>xml</span><span>></span>
    <span><</span><span>ToUserName</span><span>></span><span><![CDATA[</span><span>gh_680bdefc8c5d</span><span>]]></span><span></</span><span>ToUserName</span><span>></span>
    <span><</span><span>Encrypt</span><span>></span><span><![CDATA[</span><span>MNn4+jJ/VsFh2gUyKAaOJArwEVYCvVmyN0iXzNarP3O6vXzK62ft1/KG2/XPZ4y5bPWU/jfIfQxODRQ7sLkUsrDRqsWimuhIT8Eq+w4E/28m+XDAQKEOjWTQIOp1p6kNsIV1DdC3B+AtcKcKSNAeJDr7x7GHLx5DZYK09qQsYDOjP6R5NqebFjKt/NpEl/GU3gWFwG8LCtRNuIYdK5axbFSfmXbh5CZ6Bk5wSwj5fu5aS90cMAgUhGsxrxZTY562QR6c+3ydXxb+GHI5w+qA+eqJjrQqR7u5hS+1x5sEsA7vS+bZ5LYAR3+PZ243avQkGllQ+rg7a6TeSGDxxhvLw+mxxinyk88BNHkJnyK//hM1k9PuvuLAASdaud4vzRQlAmnYOslZl8CN7gjCjV41skUTZv3wwGPxvEqtm/nf5fQ=</span><span>]]></span><span></</span><span>Encrypt</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

At this time, the program needs to obtain the following parameters from the url

<span>$timestamp</span>  = <span>$_GET</span>['timestamp'<span>];
</span><span>$nonce</span> = <span>$_GET</span>["nonce"<span>];
</span><span>$msg_signature</span>  = <span>$_GET</span>['msg_signature'<span>];
</span><span>$encrypt_type</span> = <span>$_GET</span>['encrypt_type'];
Copy after login

These parameters will be used in the encryption and decryption process

After receiving the message, first decrypt it. Part of the decryption code is as follows

<span>$postStr</span> = <span>$GLOBALS</span>["HTTP_RAW_POST_DATA"<span>];
</span><span>if</span> (<span>$encrypt_type</span> == 'aes'<span>){
    </span><span>$pc</span> = <span>new</span> WXBizMsgCrypt(TOKEN, EncodingAESKey,<span> AppID);                
    </span><span>$this</span>->logger(" D \r\n".<span>$postStr</span><span>);
    </span><span>$decryptMsg</span> = "";  <span>//</span><span>解密后的明文</span>
    <span>$errCode</span> = <span>$pc</span>->DecryptMsg(<span>$msg_signature</span>, <span>$timestamp</span>, <span>$nonce</span>, <span>$postStr</span>, <span>$decryptMsg</span><span>);
    </span><span>$postStr</span> = <span>$decryptMsg</span><span>;
}</span>
Copy after login

After decryption is completed, the decrypted content is returned to $postStr. This is to ensure that the decrypted content in the message is unified with the message in plaintext mode to facilitate subsequent processing. The decrypted XML is as follows

<span><</span><span>xml</span><span>></span>
    <span><</span><span>ToUserName</span><span>></span><span><![CDATA[</span><span>gh_680bdefc8c5d</span><span>]]></span><span></</span><span>ToUserName</span><span>></span>
    <span><</span><span>FromUserName</span><span>></span><span><![CDATA[</span><span>oIDrpjpQ8j8mBuQ8nM26HWzNEZgg</span><span>]]></span><span></</span><span>FromUserName</span><span>></span>
    <span><</span><span>CreateTime</span><span>></span>1414243737<span></</span><span>CreateTime</span><span>></span>
    <span><</span><span>MsgType</span><span>></span><span><![CDATA[</span><span>text</span><span>]]></span><span></</span><span>MsgType</span><span>></span>
    <span><</span><span>Content</span><span>></span><span><![CDATA[</span><span>?</span><span>]]></span><span></</span><span>Content</span><span>></span>
    <span><</span><span>MsgId</span><span>></span>6074130599188426998<span></</span><span>MsgId</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

Process the message in your own original code. After completion, the message to be replied is as follows

<span><</span><span>xml</span><span>></span>
    <span><</span><span>ToUserName</span><span>></span><span><![CDATA[</span><span>oIDrpjpQ8j8mBuQ8nM26HWzNEZgg</span><span>]]></span><span></</span><span>ToUserName</span><span>></span>
    <span><</span><span>FromUserName</span><span>></span><span><![CDATA[</span><span>gh_680bdefc8c5d</span><span>]]></span><span></</span><span>FromUserName</span><span>></span>
    <span><</span><span>CreateTime</span><span>></span>1414243733<span></</span><span>CreateTime</span><span>></span>
    <span><</span><span>MsgType</span><span>></span><span><![CDATA[</span><span>text</span><span>]]></span><span></</span><span>MsgType</span><span>></span>
    <span><</span><span>Content</span><span>></span><span><![CDATA[</span><span>2014-10-25 21:28:53
技术支持 方倍工作室
http://www.fangbei.org/</span><span>]]></span><span></</span><span>Content</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

Encrypt the above message and return it to the WeChat public account

<span>//</span><span>加密</span>
<span>if</span> (<span>$encrypt_type</span> == 'aes'<span>){
    </span><span>$encryptMsg</span> = ''; <span>//</span><span>加密后的密文</span>
    <span>$errCode</span> = <span>$pc</span>->encryptMsg(<span>$result</span>, <span>$timeStamp</span>, <span>$nonce</span>, <span>$encryptMsg</span><span>);
    </span><span>$result</span> = <span>$encryptMsg</span><span>;
    </span><span>$this</span>->logger(" E \r\n".<span>$result</span><span>);
}</span>
Copy after login

The encrypted content is as follows

<span><</span><span>xml</span><span>></span>
    <span><</span><span>Encrypt</span><span>></span><span><![CDATA[</span><span>pE6gp6qvVBMHwCXwnM7illFBrh9LmvlKFlPUDuyQo9EKNunqbUFMd2KjiYoz+3K1B+93JbMWHt+19TI8awdRdyopRS4oUNg5M2jwpwXTmc6TtafkKNjvqlvPXIWmutw0tuMXke1hDgsqz0SC8h/QjNLxECuwnczrfCMJlt+APHnX2yMMaq/aYUNcndOH387loQvl2suCGucXpglnbxf7frTCz9NQVgKiYrvKOhk6KFiVMnzuxy6WWmoe3GBiUCPTtYf5b1CxzN2IHViEBm28ilV9wWdNOM9TPG7BSSAcpgY4pcwdIG5+4KhgYmnVU3bc/ZJkk42TIdidigOfFpJwET4UWVrLB/ldUud4aPexp3aPCR3Fe53S2HHcl3tTxh4iRvDftUKP3svYPctt1MlYuYv/BZ4JyzUQV03H+0XrVyDY2tyVjimgCrA2c1mZMgHttOHTQ6VTnxrMq0GWlRlH0KPQKqtjUpNQzuOH4upQ8boPsEtuY3wDA2RaXQPJrXon</span><span>]]></span><span></</span><span>Encrypt</span><span>></span>
    <span><</span><span>MsgSignature</span><span>></span><span><![CDATA[</span><span>6c46904dc1f58b2ddf2dd0399f1c6cf41f33ecb9</span><span>]]></span><span></</span><span>MsgSignature</span><span>></span>
    <span><</span><span>TimeStamp</span><span>></span>1414243733<span></</span><span>TimeStamp</span><span>></span>
    <span><</span><span>Nonce</span><span>></span><span><![CDATA[</span><span>1792106704</span><span>]]></span><span></</span><span>Nonce</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

In this way, the encryption and decryption of messages in safe mode is completed.

3. Complete code

<span>  1</span> <?<span>php
</span><span>  2</span> <span>/*</span>
<span>  3</span> <span>    方倍工作室 http://www.cnblogs.com/txw1958/
</span><span>  4</span> <span>    CopyRight 2014 All Rights Reserved
</span><span>  5</span> <span>*/</span>
<span>  6</span> <span>define</span>("TOKEN", "weixin"<span>);
</span><span>  7</span> <span>define</span>("AppID", "wxbad0b45542aa0b5e"<span>);
</span><span>  8</span> <span>define</span>("EncodingAESKey", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"<span>);
</span><span>  9</span> <span>require_once</span>('wxBizMsgCrypt.php'<span>);
</span><span> 10</span> 
<span> 11</span> <span>$wechatObj</span> = <span>new</span><span> wechatCallbackapiTest();
</span><span> 12</span> <span>if</span> (!<span>isset</span>(<span>$_GET</span>['echostr'<span>])) {
</span><span> 13</span>     <span>$wechatObj</span>-><span>responseMsg();
</span><span> 14</span> }<span>else</span><span>{
</span><span> 15</span>     <span>$wechatObj</span>-><span>valid();
</span><span> 16</span> <span>}
</span><span> 17</span> 
<span> 18</span> <span>class</span><span> wechatCallbackapiTest
</span><span> 19</span> <span>{
</span><span> 20</span>     <span>//</span><span>验证签名</span>
<span> 21</span>     <span>public</span> <span>function</span><span> valid()
</span><span> 22</span> <span>    {
</span><span> 23</span>         <span>$echoStr</span> = <span>$_GET</span>["echostr"<span>];
</span><span> 24</span>         <span>$signature</span> = <span>$_GET</span>["signature"<span>];
</span><span> 25</span>         <span>$timestamp</span> = <span>$_GET</span>["timestamp"<span>];
</span><span> 26</span>         <span>$nonce</span> = <span>$_GET</span>["nonce"<span>];
</span><span> 27</span>         <span>$tmpArr</span> = <span>array</span>(TOKEN, <span>$timestamp</span>, <span>$nonce</span><span>);
</span><span> 28</span>         <span>sort</span>(<span>$tmpArr</span><span>);
</span><span> 29</span>         <span>$tmpStr</span> = <span>implode</span>(<span>$tmpArr</span><span>);
</span><span> 30</span>         <span>$tmpStr</span> = <span>sha1</span>(<span>$tmpStr</span><span>);
</span><span> 31</span>         <span>if</span>(<span>$tmpStr</span> == <span>$signature</span><span>){
</span><span> 32</span>             <span>echo</span> <span>$echoStr</span><span>;
</span><span> 33</span>             <span>exit</span><span>;
</span><span> 34</span> <span>        }
</span><span> 35</span> <span>    }
</span><span> 36</span> 
<span> 37</span>     <span>//</span><span>响应消息</span>
<span> 38</span>     <span>public</span> <span>function</span><span> responseMsg()
</span><span> 39</span> <span>    {
</span><span> 40</span>         <span>$timestamp</span>  = <span>$_GET</span>['timestamp'<span>];
</span><span> 41</span>         <span>$nonce</span> = <span>$_GET</span>["nonce"<span>];
</span><span> 42</span>         <span>$msg_signature</span>  = <span>$_GET</span>['msg_signature'<span>];
</span><span> 43</span>         <span>$encrypt_type</span> = (<span>isset</span>(<span>$_GET</span>['encrypt_type']) && (<span>$_GET</span>['encrypt_type'] == 'aes')) ? "aes" : "raw"<span>;
</span><span> 44</span>         
<span> 45</span>         <span>$postStr</span> = <span>$GLOBALS</span>["HTTP_RAW_POST_DATA"<span>];
</span><span> 46</span>         <span>if</span> (!<span>empty</span>(<span>$postStr</span><span>)){
</span><span> 47</span>             <span>//</span><span>解密</span>
<span> 48</span>             <span>if</span> (<span>$encrypt_type</span> == 'aes'<span>){
</span><span> 49</span>                 <span>$pc</span> = <span>new</span> WXBizMsgCrypt(TOKEN, EncodingAESKey,<span> AppID);                
</span><span> 50</span>                 <span>$this</span>->logger(" D \r\n".<span>$postStr</span><span>);
</span><span> 51</span>                 <span>$decryptMsg</span> = "";  <span>//</span><span>解密后的明文</span>
<span> 52</span>                 <span>$errCode</span> = <span>$pc</span>->DecryptMsg(<span>$msg_signature</span>, <span>$timestamp</span>, <span>$nonce</span>, <span>$postStr</span>, <span>$decryptMsg</span><span>);
</span><span> 53</span>                 <span>$postStr</span> = <span>$decryptMsg</span><span>;
</span><span> 54</span> <span>            }
</span><span> 55</span>             <span>$this</span>->logger(" R \r\n".<span>$postStr</span><span>);
</span><span> 56</span>             <span>$postObj</span> = <span>simplexml_load_string</span>(<span>$postStr</span>, 'SimpleXMLElement',<span> LIBXML_NOCDATA);
</span><span> 57</span>             <span>$RX_TYPE</span> = <span>trim</span>(<span>$postObj</span>-><span>MsgType);
</span><span> 58</span> 
<span> 59</span>             <span>//</span><span>消息类型分离</span>
<span> 60</span>             <span>switch</span> (<span>$RX_TYPE</span><span>)
</span><span> 61</span> <span>            {
</span><span> 62</span>                 <span>case</span> "event":
<span> 63</span>                     <span>$result</span> = <span>$this</span>->receiveEvent(<span>$postObj</span><span>);
</span><span> 64</span>                     <span>break</span><span>;
</span><span> 65</span>                 <span>case</span> "text":
<span> 66</span>                     <span>$result</span> = <span>$this</span>->receiveText(<span>$postObj</span><span>);
</span><span> 67</span>                     <span>break</span><span>;
</span><span> 68</span> <span>            }
</span><span> 69</span>             <span>$this</span>->logger(" R \r\n".<span>$result</span><span>);
</span><span> 70</span>             <span>//</span><span>加密</span>
<span> 71</span>             <span>if</span> (<span>$encrypt_type</span> == 'aes'<span>){
</span><span> 72</span>                 <span>$encryptMsg</span> = ''; <span>//</span><span>加密后的密文</span>
<span> 73</span>                 <span>$errCode</span> = <span>$pc</span>->encryptMsg(<span>$result</span>, <span>$timeStamp</span>, <span>$nonce</span>, <span>$encryptMsg</span><span>);
</span><span> 74</span>                 <span>$result</span> = <span>$encryptMsg</span><span>;
</span><span> 75</span>                 <span>$this</span>->logger(" E \r\n".<span>$result</span><span>);
</span><span> 76</span> <span>            }
</span><span> 77</span>             <span>echo</span> <span>$result</span><span>;
</span><span> 78</span>         }<span>else</span><span> {
</span><span> 79</span>             <span>echo</span> ""<span>;
</span><span> 80</span>             <span>exit</span><span>;
</span><span> 81</span> <span>        }
</span><span> 82</span> <span>    }
</span><span> 83</span> 
<span> 84</span>     <span>//</span><span>接收事件消息</span>
<span> 85</span>     <span>private</span> <span>function</span> receiveEvent(<span>$object</span><span>)
</span><span> 86</span> <span>    {
</span><span> 87</span>         <span>$content</span> = ""<span>;
</span><span> 88</span>         <span>switch</span> (<span>$object</span>-><span>Event)
</span><span> 89</span> <span>        {
</span><span> 90</span>             <span>case</span> "subscribe":
<span> 91</span>                 <span>$content</span> = "欢迎关注方倍工作室 "<span>;
</span><span> 92</span>                 <span>break</span><span>;
</span><span> 93</span> <span>        }
</span><span> 94</span> 
<span> 95</span>         <span>$result</span> = <span>$this</span>->transmitText(<span>$object</span>, <span>$content</span><span>);
</span><span> 96</span>         <span>return</span> <span>$result</span><span>;
</span><span> 97</span> <span>    }
</span><span> 98</span> 
<span> 99</span>     <span>//</span><span>接收文本消息</span>
<span>100</span>     <span>private</span> <span>function</span> receiveText(<span>$object</span><span>)
</span><span>101</span> <span>    {
</span><span>102</span>         <span>$keyword</span> = <span>trim</span>(<span>$object</span>-><span>Content);
</span><span>103</span>         <span>if</span> (<span>strstr</span>(<span>$keyword</span>, "文本"<span>)){
</span><span>104</span>             <span>$content</span> = "这是个文本消息"<span>;
</span><span>105</span>         }<span>else</span> <span>if</span> (<span>strstr</span>(<span>$keyword</span>, "单图文"<span>)){
</span><span>106</span>             <span>$content</span> = <span>array</span><span>();
</span><span>107</span>             <span>$content</span>[] = <span>array</span>("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"<span>);
</span><span>108</span>         }<span>else</span> <span>if</span> (<span>strstr</span>(<span>$keyword</span>, "图文") || <span>strstr</span>(<span>$keyword</span>, "多图文"<span>)){
</span><span>109</span>             <span>$content</span> = <span>array</span><span>();
</span><span>110</span>             <span>$content</span>[] = <span>array</span>("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"<span>);
</span><span>111</span>             <span>$content</span>[] = <span>array</span>("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"<span>);
</span><span>112</span>             <span>$content</span>[] = <span>array</span>("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"<span>);
</span><span>113</span>         }<span>else</span> <span>if</span> (<span>strstr</span>(<span>$keyword</span>, "音乐"<span>)){
</span><span>114</span>             <span>$content</span> = <span>array</span><span>();
</span><span>115</span>             <span>$content</span> = <span>array</span>("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"<span>);
</span><span>116</span>         }<span>else</span><span>{
</span><span>117</span>             <span>$content</span> = <span>date</span>("Y-m-d H:i:s",<span>time</span>())."\n".<span>$object</span>->FromUserName."\n技术支持 方倍工作室"<span>;
</span><span>118</span> <span>        }
</span><span>119</span> 
<span>120</span>         <span>if</span>(<span>is_array</span>(<span>$content</span><span>)){
</span><span>121</span>             <span>if</span> (<span>isset</span>(<span>$content</span>[0<span>])){
</span><span>122</span>                 <span>$result</span> = <span>$this</span>->transmitNews(<span>$object</span>, <span>$content</span><span>);
</span><span>123</span>             }<span>else</span> <span>if</span> (<span>isset</span>(<span>$content</span>['MusicUrl'<span>])){
</span><span>124</span>                 <span>$result</span> = <span>$this</span>->transmitMusic(<span>$object</span>, <span>$content</span><span>);
</span><span>125</span> <span>            }
</span><span>126</span>         }<span>else</span><span>{
</span><span>127</span>             <span>$result</span> = <span>$this</span>->transmitText(<span>$object</span>, <span>$content</span><span>);
</span><span>128</span> <span>        }
</span><span>129</span>         <span>return</span> <span>$result</span><span>;
</span><span>130</span> <span>    }
</span><span>131</span> 
<span>132</span>     <span>//</span><span>回复文本消息</span>
<span>133</span>     <span>private</span> <span>function</span> transmitText(<span>$object</span>, <span>$content</span><span>)
</span><span>134</span> <span>    {
</span><span>135</span>         <span>$xmlTpl</span> = "<span><xml>
</span><span>136</span> <span>    <ToUserName><![CDATA[%s]]></ToUserName>
</span><span>137</span> <span>    <FromUserName><![CDATA[%s]]></FromUserName>
</span><span>138</span> <span>    <CreateTime>%s</CreateTime>
</span><span>139</span> <span>    <MsgType><![CDATA[text]]></MsgType>
</span><span>140</span> <span>    <Content><![CDATA[%s]]></Content>
</span><span>141</span> </xml>"<span>;
</span><span>142</span>         <span>$result</span> = <span>sprintf</span>(<span>$xmlTpl</span>, <span>$object</span>->FromUserName, <span>$object</span>->ToUserName, <span>time</span>(), <span>$content</span><span>);
</span><span>143</span>         <span>return</span> <span>$result</span><span>;
</span><span>144</span> <span>    }
</span><span>145</span> 
<span>146</span>     <span>//</span><span>回复图文消息</span>
<span>147</span>     <span>private</span> <span>function</span> transmitNews(<span>$object</span>, <span>$newsArray</span><span>)
</span><span>148</span> <span>    {
</span><span>149</span>         <span>if</span>(!<span>is_array</span>(<span>$newsArray</span><span>)){
</span><span>150</span>             <span>return</span><span>;
</span><span>151</span> <span>        }
</span><span>152</span>         <span>$itemTpl</span> = "<span>        <item>
</span><span>153</span> <span>            <Title><![CDATA[%s]]></Title>
</span><span>154</span> <span>            <Description><![CDATA[%s]]></Description>
</span><span>155</span> <span>            <PicUrl><![CDATA[%s]]></PicUrl>
</span><span>156</span> <span>            <Url><![CDATA[%s]]></Url>
</span><span>157</span> <span>        </item>
</span><span>158</span> "<span>;
</span><span>159</span>         <span>$item_str</span> = ""<span>;
</span><span>160</span>         <span>foreach</span> (<span>$newsArray</span> <span>as</span> <span>$item</span><span>){
</span><span>161</span>             <span>$item_str</span> .= <span>sprintf</span>(<span>$itemTpl</span>, <span>$item</span>['Title'], <span>$item</span>['Description'], <span>$item</span>['PicUrl'], <span>$item</span>['Url'<span>]);
</span><span>162</span> <span>        }
</span><span>163</span>         <span>$xmlTpl</span> = "<span><xml>
</span><span>164</span> <span>    <ToUserName><![CDATA[%s]]></ToUserName>
</span><span>165</span> <span>    <FromUserName><![CDATA[%s]]></FromUserName>
</span><span>166</span> <span>    <CreateTime>%s</CreateTime>
</span><span>167</span> <span>    <MsgType><![CDATA[news]]></MsgType>
</span><span>168</span> <span>    <ArticleCount>%s</ArticleCount>
</span><span>169</span> <span>    <Articles>
</span><span>170</span> <span>$item_str</span><span>    </Articles>
</span><span>171</span> </xml>"<span>;
</span><span>172</span> 
<span>173</span>         <span>$result</span> = <span>sprintf</span>(<span>$xmlTpl</span>, <span>$object</span>->FromUserName, <span>$object</span>->ToUserName, <span>time</span>(), <span>count</span>(<span>$newsArray</span><span>));
</span><span>174</span>         <span>return</span> <span>$result</span><span>;
</span><span>175</span> <span>    }
</span><span>176</span> 
<span>177</span>     <span>//</span><span>回复音乐消息</span>
<span>178</span>     <span>private</span> <span>function</span> transmitMusic(<span>$object</span>, <span>$musicArray</span><span>)
</span><span>179</span> <span>    {
</span><span>180</span>         <span>$itemTpl</span> = "<span><Music>
</span><span>181</span> <span>        <Title><![CDATA[%s]]></Title>
</span><span>182</span> <span>        <Description><![CDATA[%s]]></Description>
</span><span>183</span> <span>        <MusicUrl><![CDATA[%s]]></MusicUrl>
</span><span>184</span> <span>        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</span><span>185</span>     </Music>"<span>;
</span><span>186</span> 
<span>187</span>         <span>$item_str</span> = <span>sprintf</span>(<span>$itemTpl</span>, <span>$musicArray</span>['Title'], <span>$musicArray</span>['Description'], <span>$musicArray</span>['MusicUrl'], <span>$musicArray</span>['HQMusicUrl'<span>]);
</span><span>188</span> 
<span>189</span>         <span>$xmlTpl</span> = "<span><xml>
</span><span>190</span> <span>    <ToUserName><![CDATA[%s]]></ToUserName>
</span><span>191</span> <span>    <FromUserName><![CDATA[%s]]></FromUserName>
</span><span>192</span> <span>    <CreateTime>%s</CreateTime>
</span><span>193</span> <span>    <MsgType><![CDATA[music]]></MsgType>
</span><span>194</span>     <span>$item_str</span>
<span>195</span> </xml>"<span>;
</span><span>196</span> 
<span>197</span>         <span>$result</span> = <span>sprintf</span>(<span>$xmlTpl</span>, <span>$object</span>->FromUserName, <span>$object</span>->ToUserName, <span>time</span><span>());
</span><span>198</span>         <span>return</span> <span>$result</span><span>;
</span><span>199</span> <span>    }
</span><span>200</span> 
<span>201</span>     <span>//</span><span>日志记录</span>
<span>202</span>     <span>public</span> <span>function</span> logger(<span>$log_content</span><span>)
</span><span>203</span> <span>    {
</span><span>204</span>         <span>if</span>(<span>isset</span>(<span>$_SERVER</span>['HTTP_APPNAME'])){   <span>//</span><span>SAE</span>
<span>205</span>             sae_set_display_errors(<span>false</span><span>);
</span><span>206</span>             sae_debug(<span>$log_content</span><span>);
</span><span>207</span>             sae_set_display_errors(<span>true</span><span>);
</span><span>208</span>         }<span>else</span> <span>if</span>(<span>$_SERVER</span>['REMOTE_ADDR'] != "127.0.0.1"){ <span>//</span><span>LOCAL</span>
<span>209</span>             <span>$max_size</span> = 500000<span>;
</span><span>210</span>             <span>$log_filename</span> = "log.xml"<span>;
</span><span>211</span>             <span>if</span>(<span>file_exists</span>(<span>$log_filename</span>) and (<span>abs</span>(<span>filesize</span>(<span>$log_filename</span>)) > <span>$max_size</span>)){<span>unlink</span>(<span>$log_filename</span><span>);}
</span><span>212</span>             <span>file_put_contents</span>(<span>$log_filename</span>, <span>date</span>('Y-m-d H:i:s').<span>$log_content</span>."\r\n",<span> FILE_APPEND);
</span><span>213</span> <span>        }
</span><span>214</span> <span>    }
</span><span>215</span> <span>}
</span><span>216</span> ?>
Copy after login

How to send messages on WeChat public platform

The picture and text messages on the WeChat public platform are sent like this:

1. First click on the material management item under the left column management column, and then select "Single picture and text message" or " "Multi-image and text message" to enter the editing interface. (A single image and text message is suitable for one message and one topic, and a multi-image and text message is suitable for one message and multiple topics)

2. After entering the editing interface, add the corresponding title, cover image, and summary (multiple image and text messages are not applicable) item) and the text of the message and save it; if you want to see the edited effect, you can preview it, enter your personal WeChat ID, and let the message be sent to your mobile phone to see the effect.

3. After entering the group sending function and setting the target, gender, and region of the group sending, select the last "graphic message" on the internal customer option below, then select the edit you just made in the material management and save it. After checking the picture and text message, click the green group sending button below to send the message.

If you have any questions, please ask or add me. . .

How to send messages on WeChat public platform

The graphic messages on the WeChat public platform are sent as follows:
1. First click on the material management item under the left column management column, and then select "Single graphic message" or "Multiple graphic messages" in the middle editing area Message" to enter the editing interface. (A single image and text message is suitable for one message and one topic, and a multi-image and text message is suitable for one message and multiple topics)
2. After entering the editing interface, add the corresponding title, cover image, and summary (multiple image and text messages have no items) and Save the text of the message; if you want to see the edited effect, you can choose to preview it, enter your personal WeChat ID, and have the message sent to your mobile phone to see the effect.
3. After entering the group sending function and setting the target, gender, and region of the group sending, select the last "graphic message" on the internal customer option below, and then select the graphic and text you just edited and saved in the material management. After checking the message, click the green group sending button below to send the message.

If you don’t understand, please feel free to add me or ask. . . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/899963.htmlTechArticleWeChat public platform message body encryption and decryption implementation, public decryption keyword: WeChat public platform message body signature message body encryption and decryption EncodingAESKey safe mode original text http://www.cnblogs.co...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to choose H5 and applets How to choose H5 and applets Apr 06, 2025 am 10:51 AM

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

What are the development tools for H5 and mini program? What are the development tools for H5 and mini program? Apr 06, 2025 am 09:54 AM

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.

See all articles