search
HomeBackend DevelopmentPHP TutorialImplementation of message body encryption and decryption on WeChat public platform, public decryption_PHP tutorial

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>

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');

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>

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>

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'];

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>

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>

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>

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>

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>

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> ?>

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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.