The official SDK only has a text message function. We have sorted out the message types and event responses of all messages, and added log records. The code is as follows:
<span 1</span> <?<span php </span><span 2</span> <span /*</span> <span 3</span> <span 方倍工作室 </span><span 4</span> <span CopyRight 2014 All Rights Reserved </span><span 5</span> <span */</span> <span 6</span> <span 7</span> <span define</span>("TOKEN", "weixin"<span ); </span><span 8</span> <span 9</span> <span $wechatObj</span> = <span new</span><span wechatCallbackapiTest(); </span><span 10</span> <span if</span> (!<span isset</span>(<span $_GET</span>['echostr'<span ])) { </span><span 11</span> <span $wechatObj</span>-><span responseMsg(); </span><span 12</span> }<span else</span><span { </span><span 13</span> <span $wechatObj</span>-><span valid(); </span><span 14</span> <span } </span><span 15</span> <span 16</span> <span class</span><span wechatCallbackapiTest </span><span 17</span> <span { </span><span 18</span> <span public</span> <span function</span><span valid() </span><span 19</span> <span { </span><span 20</span> <span $echoStr</span> = <span $_GET</span>["echostr"<span ]; </span><span 21</span> <span if</span>(<span $this</span>-><span checkSignature()){ </span><span 22</span> <span echo</span> <span $echoStr</span><span ; </span><span 23</span> <span exit</span><span ; </span><span 24</span> <span } </span><span 25</span> <span } </span><span 26</span> <span 27</span> <span private</span> <span function</span><span checkSignature() </span><span 28</span> <span { </span><span 29</span> <span $signature</span> = <span $_GET</span>["signature"<span ]; </span><span 30</span> <span $timestamp</span> = <span $_GET</span>["timestamp"<span ]; </span><span 31</span> <span $nonce</span> = <span $_GET</span>["nonce"<span ]; </span><span 32</span> <span $token</span> =<span TOKEN; </span><span 33</span> <span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span ); </span><span 34</span> <span sort</span>(<span $tmpArr</span>,<span SORT_STRING); </span><span 35</span> <span $tmpStr</span> = <span implode</span>(<span $tmpArr</span><span ); </span><span 36</span> <span $tmpStr</span> = <span sha1</span>(<span $tmpStr</span><span ); </span><span 37</span> <span 38</span> <span if</span>(<span $tmpStr</span> == <span $signature</span><span ){ </span><span 39</span> <span return</span> <span true</span><span ; </span><span 40</span> }<span else</span><span { </span><span 41</span> <span return</span> <span false</span><span ; </span><span 42</span> <span } </span><span 43</span> <span } </span><span 44</span> <span 45</span> <span public</span> <span function</span><span responseMsg() </span><span 46</span> <span { </span><span 47</span> <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ]; </span><span 48</span> <span if</span> (!<span empty</span>(<span $postStr</span><span )){ </span><span 49</span> <span $this</span>->logger("R ".<span $postStr</span><span ); </span><span 50</span> <span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span LIBXML_NOCDATA); </span><span 51</span> <span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType); </span><span 52</span> <span 53</span> <span switch</span> (<span $RX_TYPE</span><span ) </span><span 54</span> <span { </span><span 55</span> <span case</span> "event": <span 56</span> <span $result</span> = <span $this</span>->receiveEvent(<span $postObj</span><span ); </span><span 57</span> <span break</span><span ; </span><span 58</span> <span case</span> "text": <span 59</span> <span $result</span> = <span $this</span>->receiveText(<span $postObj</span><span ); </span><span 60</span> <span break</span><span ; </span><span 61</span> <span case</span> "image": <span 62</span> <span $result</span> = <span $this</span>->receiveImage(<span $postObj</span><span ); </span><span 63</span> <span break</span><span ; </span><span 64</span> <span case</span> "location": <span 65</span> <span $result</span> = <span $this</span>->receiveLocation(<span $postObj</span><span ); </span><span 66</span> <span break</span><span ; </span><span 67</span> <span case</span> "voice": <span 68</span> <span $result</span> = <span $this</span>->receiveVoice(<span $postObj</span><span ); </span><span 69</span> <span break</span><span ; </span><span 70</span> <span case</span> "video": <span 71</span> <span $result</span> = <span $this</span>->receiveVideo(<span $postObj</span><span ); </span><span 72</span> <span break</span><span ; </span><span 73</span> <span case</span> "link": <span 74</span> <span $result</span> = <span $this</span>->receiveLink(<span $postObj</span><span ); </span><span 75</span> <span break</span><span ; </span><span 76</span> <span default</span>: <span 77</span> <span $result</span> = "unknow msg type: ".<span $RX_TYPE</span><span ; </span><span 78</span> <span break</span><span ; </span><span 79</span> <span } </span><span 80</span> <span $this</span>->logger("T ".<span $result</span><span ); </span><span 81</span> <span echo</span> <span $result</span><span ; </span><span 82</span> }<span else</span><span { </span><span 83</span> <span echo</span> ""<span ; </span><span 84</span> <span exit</span><span ; </span><span 85</span> <span } </span><span 86</span> <span } </span><span 87</span> <span 88</span> <span private</span> <span function</span> receiveEvent(<span $object</span><span ) </span><span 89</span> <span { </span><span 90</span> <span $content</span> = ""<span ; </span><span 91</span> <span switch</span> (<span $object</span>-><span Event) </span><span 92</span> <span { </span><span 93</span> <span case</span> "subscribe": <span 94</span> <span $content</span> = "欢迎关注方倍工作室 "<span ; </span><span 95</span> <span $content</span> .= (!<span empty</span>(<span $object</span>->EventKey))?("\n来自二维码场景 ".<span str_replace</span>("qrscene_","",<span $object</span>->EventKey)):""<span ; </span><span 96</span> <span break</span><span ; </span><span 97</span> <span case</span> "unsubscribe": <span 98</span> <span $content</span> = "取消关注"<span ; </span><span 99</span> <span break</span><span ; </span><span 100</span> <span case</span> "SCAN": <span 101</span> <span $content</span> = "扫描场景 ".<span $object</span>-><span EventKey; </span><span 102</span> <span break</span><span ; </span><span 103</span> <span case</span> "CLICK": <span 104</span> <span switch</span> (<span $object</span>-><span EventKey) </span><span 105</span> <span { </span><span 106</span> <span case</span> "COMPANY": <span 107</span> <span $content</span> = "方倍工作室提供互联网相关产品与服务。"<span ; </span><span 108</span> <span break</span><span ; </span><span 109</span> <span default</span>: <span 110</span> <span $content</span> = "点击菜单:".<span $object</span>-><span EventKey; </span><span 111</span> <span break</span><span ; </span><span 112</span> <span } </span><span 113</span> <span break</span><span ; </span><span 114</span> <span case</span> "LOCATION": <span 115</span> <span $content</span> = "上传位置:纬度 ".<span $object</span>->Latitude.";经度 ".<span $object</span>-><span Longitude; </span><span 116</span> <span break</span><span ; </span><span 117</span> <span default</span>: <span 118</span> <span $content</span> = "receive a new event: ".<span $object</span>-><span Event; </span><span 119</span> <span break</span><span ; </span><span 120</span> <span } </span><span 121</span> <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span 122</span> <span return</span> <span $result</span><span ; </span><span 123</span> <span } </span><span 124</span> <span 125</span> <span private</span> <span function</span> receiveText(<span $object</span><span ) </span><span 126</span> <span { </span><span 127</span> <span $keyword</span> = <span trim</span>(<span $object</span>-><span Content); </span><span 128</span> <span switch</span> (<span $keyword</span><span ) </span><span 129</span> <span { </span><span 130</span> <span case</span> "文本": <span 131</span> <span $content</span> = "这是个文本消息"<span ; </span><span 132</span> <span break</span><span ; </span><span 133</span> <span case</span> "图文": <span 134</span> <span case</span> "单图文": <span 135</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 136</span> <span break</span><span ; </span><span 137</span> <span case</span> "多图文": <span 138</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 139</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 140</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 141</span> <span break</span><span ; </span><span 142</span> <span case</span> "音乐": <span 143</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 144</span> <span break</span><span ; </span><span 145</span> <span default</span>: <span 146</span> <span $content</span> = "当前时间:".<span date</span>("Y-m-d H:i:s",<span time</span><span ()); </span><span 147</span> <span break</span><span ; </span><span 148</span> <span } </span><span 149</span> <span if</span>(<span is_array</span>(<span $content</span><span )){ </span><span 150</span> <span if</span> (<span isset</span>(<span $content</span>[0]['PicUrl'<span ])){ </span><span 151</span> <span $result</span> = <span $this</span>->transmitNews(<span $object</span>, <span $content</span><span ); </span><span 152</span> }<span else</span> <span if</span> (<span isset</span>(<span $content</span>['MusicUrl'<span ])){ </span><span 153</span> <span $result</span> = <span $this</span>->transmitMusic(<span $object</span>, <span $content</span><span ); </span><span 154</span> <span } </span><span 155</span> }<span else</span><span { </span><span 156</span> <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span 157</span> <span } </span><span 158</span> <span return</span> <span $result</span><span ; </span><span 159</span> <span } </span><span 160</span> <span 161</span> <span private</span> <span function</span> receiveImage(<span $object</span><span ) </span><span 162</span> <span { </span><span 163</span> <span $content</span> = <span array</span>("MediaId"=><span $object</span>-><span MediaId); </span><span 164</span> <span $result</span> = <span $this</span>->transmitImage(<span $object</span>, <span $content</span><span ); </span><span 165</span> <span return</span> <span $result</span><span ; </span><span 166</span> <span } </span><span 167</span> <span 168</span> <span private</span> <span function</span> receiveLocation(<span $object</span><span ) </span><span 169</span> <span { </span><span 170</span> <span $content</span> = "你发送的是位置,纬度为:".<span $object</span>->Location_X.";经度为:".<span $object</span>->Location_Y.";缩放级别为:".<span $object</span>->Scale.";位置为:".<span $object</span>-><span Label; </span><span 171</span> <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span 172</span> <span return</span> <span $result</span><span ; </span><span 173</span> <span } </span><span 174</span> <span 175</span> <span private</span> <span function</span> receiveVoice(<span $object</span><span ) </span><span 176</span> <span { </span><span 177</span> <span if</span> (<span empty</span>(<span $object</span>-><span Recognition)){ </span><span 178</span> <span $content</span> = <span array</span>("MediaId"=><span $object</span>-><span MediaId); </span><span 179</span> <span $result</span> = <span $this</span>->transmitVoice(<span $object</span>, <span $content</span><span ); </span><span 180</span> }<span else</span><span { </span><span 181</span> <span $content</span> = "你刚才说的是:".<span $object</span>-><span Recognition; </span><span 182</span> <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span 183</span> <span } </span><span 184</span> <span 185</span> <span return</span> <span $result</span><span ; </span><span 186</span> <span } </span><span 187</span> <span 188</span> <span private</span> <span function</span> receiveVideo(<span $object</span><span ) </span><span 189</span> <span { </span><span 190</span> <span $content</span> = <span array</span>("MediaId"=><span $object</span>->MediaId, "ThumbMediaId"=><span $object</span>->ThumbMediaId, "Title"=>"", "Description"=>""<span ); </span><span 191</span> <span $result</span> = <span $this</span>->transmitVideo(<span $object</span>, <span $content</span><span ); </span><span 192</span> <span return</span> <span $result</span><span ; </span><span 193</span> <span } </span><span 194</span> <span 195</span> <span private</span> <span function</span> receiveLink(<span $object</span><span ) </span><span 196</span> <span { </span><span 197</span> <span $content</span> = "你发送的是链接,标题为:".<span $object</span>->Title.";内容为:".<span $object</span>->Description.";链接地址为:".<span $object</span>-><span Url; </span><span 198</span> <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span 199</span> <span return</span> <span $result</span><span ; </span><span 200</span> <span } </span><span 201</span> <span 202</span> <span private</span> <span function</span> transmitText(<span $object</span>, <span $content</span><span ) </span><span 203</span> <span { </span><span 204</span> <span $textTpl</span> = "<span <xml> </span><span 205</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 206</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 207</span> <span <CreateTime>%s</CreateTime> </span><span 208</span> <span <MsgType><![CDATA[text]]></MsgType> </span><span 209</span> <span <Content><![CDATA[%s]]></Content> </span><span 210</span> </xml>"<span ; </span><span 211</span> <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span><span ); </span><span 212</span> <span return</span> <span $result</span><span ; </span><span 213</span> <span } </span><span 214</span> <span 215</span> <span private</span> <span function</span> transmitImage(<span $object</span>, <span $imageArray</span><span ) </span><span 216</span> <span { </span><span 217</span> <span $itemTpl</span> = "<span <Image> </span><span 218</span> <span <MediaId><![CDATA[%s]]></MediaId> </span><span 219</span> </Image>"<span ; </span><span 220</span> <span 221</span> <span $item_str</span> = <span sprintf</span>(<span $itemTpl</span>, <span $imageArray</span>['MediaId'<span ]); </span><span 222</span> <span 223</span> <span $textTpl</span> = "<span <xml> </span><span 224</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 225</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 226</span> <span <CreateTime>%s</CreateTime> </span><span 227</span> <span <MsgType><![CDATA[image]]></MsgType> </span><span 228</span> <span $item_str</span> <span 229</span> </xml>"<span ; </span><span 230</span> <span 231</span> <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span><span ()); </span><span 232</span> <span return</span> <span $result</span><span ; </span><span 233</span> <span } </span><span 234</span> <span 235</span> <span private</span> <span function</span> transmitVoice(<span $object</span>, <span $voiceArray</span><span ) </span><span 236</span> <span { </span><span 237</span> <span $itemTpl</span> = "<span <Voice> </span><span 238</span> <span <MediaId><![CDATA[%s]]></MediaId> </span><span 239</span> </Voice>"<span ; </span><span 240</span> <span 241</span> <span $item_str</span> = <span sprintf</span>(<span $itemTpl</span>, <span $voiceArray</span>['MediaId'<span ]); </span><span 242</span> <span 243</span> <span $textTpl</span> = "<span <xml> </span><span 244</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 245</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 246</span> <span <CreateTime>%s</CreateTime> </span><span 247</span> <span <MsgType><![CDATA[voice]]></MsgType> </span><span 248</span> <span $item_str</span> <span 249</span> </xml>"<span ; </span><span 250</span> <span 251</span> <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span><span ()); </span><span 252</span> <span return</span> <span $result</span><span ; </span><span 253</span> <span } </span><span 254</span> <span 255</span> <span private</span> <span function</span> transmitVideo(<span $object</span>, <span $videoArray</span><span ) </span><span 256</span> <span { </span><span 257</span> <span $itemTpl</span> = "<span <Video> </span><span 258</span> <span <MediaId><![CDATA[%s]]></MediaId> </span><span 259</span> <span <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> </span><span 260</span> <span <Title><![CDATA[%s]]></Title> </span><span 261</span> <span <Description><![CDATA[%s]]></Description> </span><span 262</span> </Video>"<span ; </span><span 263</span> <span 264</span> <span $item_str</span> = <span sprintf</span>(<span $itemTpl</span>, <span $videoArray</span>['MediaId'], <span $videoArray</span>['ThumbMediaId'], <span $videoArray</span>['Title'], <span $videoArray</span>['Description'<span ]); </span><span 265</span> <span 266</span> <span $textTpl</span> = "<span <xml> </span><span 267</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 268</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 269</span> <span <CreateTime>%s</CreateTime> </span><span 270</span> <span <MsgType><![CDATA[video]]></MsgType> </span><span 271</span> <span $item_str</span> <span 272</span> </xml>"<span ; </span><span 273</span> <span 274</span> <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span><span ()); </span><span 275</span> <span return</span> <span $result</span><span ; </span><span 276</span> <span } </span><span 277</span> <span 278</span> <span private</span> <span function</span> transmitNews(<span $object</span>, <span $newsArray</span><span ) </span><span 279</span> <span { </span><span 280</span> <span if</span>(!<span is_array</span>(<span $newsArray</span><span )){ </span><span 281</span> <span return</span><span ; </span><span 282</span> <span } </span><span 283</span> <span $itemTpl</span> = "<span <item> </span><span 284</span> <span <Title><![CDATA[%s]]></Title> </span><span 285</span> <span <Description><![CDATA[%s]]></Description> </span><span 286</span> <span <PicUrl><![CDATA[%s]]></PicUrl> </span><span 287</span> <span <Url><![CDATA[%s]]></Url> </span><span 288</span> <span </item> </span><span 289</span> "<span ; </span><span 290</span> <span $item_str</span> = ""<span ; </span><span 291</span> <span foreach</span> (<span $newsArray</span> <span as</span> <span $item</span><span ){ </span><span 292</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 293</span> <span } </span><span 294</span> <span $newsTpl</span> = "<span <xml> </span><span 295</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 296</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 297</span> <span <CreateTime>%s</CreateTime> </span><span 298</span> <span <MsgType><![CDATA[news]]></MsgType> </span><span 299</span> <span <Content><![CDATA[]]></Content> </span><span 300</span> <span <ArticleCount>%s</ArticleCount> </span><span 301</span> <span <Articles> </span><span 302</span> <span $item_str</span><span </Articles> </span><span 303</span> </xml>"<span ; </span><span 304</span> <span 305</span> <span $result</span> = <span sprintf</span>(<span $newsTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span count</span>(<span $newsArray</span><span )); </span><span 306</span> <span return</span> <span $result</span><span ; </span><span 307</span> <span } </span><span 308</span> <span 309</span> <span private</span> <span function</span> transmitMusic(<span $object</span>, <span $musicArray</span><span ) </span><span 310</span> <span { </span><span 311</span> <span $itemTpl</span> = "<span <Music> </span><span 312</span> <span <Title><![CDATA[%s]]></Title> </span><span 313</span> <span <Description><![CDATA[%s]]></Description> </span><span 314</span> <span <MusicUrl><![CDATA[%s]]></MusicUrl> </span><span 315</span> <span <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </span><span 316</span> </Music>"<span ; </span><span 317</span> <span 318</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 319</span> <span 320</span> <span $textTpl</span> = "<span <xml> </span><span 321</span> <span <ToUserName><![CDATA[%s]]></ToUserName> </span><span 322</span> <span <FromUserName><![CDATA[%s]]></FromUserName> </span><span 323</span> <span <CreateTime>%s</CreateTime> </span><span 324</span> <span <MsgType><![CDATA[music]]></MsgType> </span><span 325</span> <span $item_str</span> <span 326</span> </xml>"<span ; </span><span 327</span> <span 328</span> <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span><span ()); </span><span 329</span> <span return</span> <span $result</span><span ; </span><span 330</span> <span } </span><span 331</span> <span 332</span> <span private</span> <span function</span> logger(<span $log_content</span><span ) </span><span 333</span> <span { </span><span 334</span> <span if</span>(<span isset</span>(<span $_SERVER</span>['HTTP_APPNAME'])){ <span //</span><span SAE</span> <span 335</span> sae_set_display_errors(<span false</span><span ); </span><span 336</span> sae_debug(<span $log_content</span><span ); </span><span 337</span> sae_set_display_errors(<span true</span><span ); </span><span 338</span> }<span else</span> <span if</span>(<span $_SERVER</span>['REMOTE_ADDR'] != "127.0.0.1"){ <span //</span><span LOCAL</span> <span 339</span> <span $max_size</span> = 10000<span ; </span><span 340</span> <span $log_filename</span> = "log.xml"<span ; </span><span 341</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 342</span> <span file_put_contents</span>(<span $log_filename</span>, <span date</span>('H:i:s')." ".<span $log_content</span>."\r\n",<span FILE_APPEND); </span><span 343</span> <span } </span><span 344</span> <span } </span><span 345</span> <span } </span><span 346</span> <span 347</span> <span 348</span> ?>