码迷,mamicode.com
首页 > 其他好文 > 详细

被动回复用户消息和关注事件推送

时间:2016-07-30 18:30:02      阅读:535      评论:0      收藏:0      [点我收藏+]

标签:

  1. /**
  2. * @Author: itwo
  3. * @Date: 2016-07-18 11:04:35
  4. * @Last Modified by: itwo
  5. * @Last Modified time: 2016-07-28 11:55:54
  6. */
  7. // 接收用户信息
  8. // 微信公众账号接收用户的消息类型判断
  9. define("TOKEN", "weixin");
  10. $wechatObj = new wechatCallbackapiTest();
  11. if(isset($_GET["echostr"])){
  12. $wechatObj->valid();
  13. }else{
  14. $wechatObj->responseMsg();
  15. }
  16. /**
  17. * 微信开发入口
  18. */
  19. class wechatCallbackapiTest
  20. {
  21. /**
  22. * 验证消息的确来自微信服务器
  23. * @return [bool] [ description ]
  24. * 转载:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
  25. */
  26. private function checkSignature()
  27. {
  28. $signature = $_GET["signature"];
  29. $timestamp = $_GET["timestamp"];
  30. $nonce = $_GET["nonce"];
  31. $token = TOKEN;
  32. $tmpArr = array($token, $timestamp, $nonce);
  33. sort($tmpArr, SORT_STRING);
  34. $tmpStr = implode($tmpArr);
  35. $tmpStr = sha1($tmpStr);
  36. if($tmpStr == $signature){
  37. return true;
  38. }else{
  39. return false;
  40. }
  41. }
  42. public function valid()
  43. {
  44. $echoStr = $_GET["echostr"];
  45. if($this->checkSignature()){
  46. echo $echoStr;
  47. exit;
  48. }
  49. }
  50. public function responseMsg()
  51. {
  52. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // 理解-> http://www.cnblogs.com/xwblog/archive/2011/12/23/2299672.html
  53. if(!empty($postStr)){
  54. $postStr = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
  55. $RX_TYPE = trim($postStr->MsgType);
  56. //用户发送的消息类型判断
  57. switch ($RX_TYPE) {
  58. case ‘text‘: // 文本消息
  59. $result = $this->receiveText($postStr);
  60. break;
  61. case ‘image‘:
  62. $result = $this->receiveImage($postStr);
  63. break;
  64. case ‘voice‘:
  65. $result = $this->receiveVoice($postStr);
  66. break;
  67. case ‘video‘:
  68. $result = $this->receiveVideo($postStr);
  69. break;
  70. case ‘shortvideo‘:
  71. $result = $this->receiveVideo($postStr);
  72. break;
  73. case ‘location‘:
  74. $result = $this->receiveLocation($postStr);
  75. break;
  76. case ‘link‘:
  77. $result = $this->receiveLink($postStr);
  78. break;
  79. case ‘event‘:
  80. $result = $this->receiveEvent($postStr);
  81. break;
  82. default:
  83. $result = $this->receiveMsgType($postStr);
  84. break;
  85. }
  86. echo $result;
  87. }else{
  88. echo "";
  89. exit;
  90. }
  91. }
  92. /**
  93. * 接收用户发送各种消息类型,提取用户发送的消息特征值,然后反馈给开发者
  94. */
  95. private function receiveText($object)
  96. {
  97. $content = " 你发送的是文本,内容为: ".$object->Content;
  98. $result = $this->transmitText($object, $content);
  99. $keyword = trim($object->Content);
  100. if ($keyword == "文本") {
  101. $content = " 你发送的是文本,内容为: ".$object->FromUserName;
  102. $result = $this->transmitText($object, $content);
  103. }
  104. else if($keyword == "图文" || $keyword == "单图文"){
  105. //回复单图文消息
  106. $content = array();
  107. $content[] = array("Title"=>"单图文标题", "Description"=>"单图文简述", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
  108. $result = $this->transmitNews($object, $content);
  109. }
  110. else if($keyword == "多图文"){
  111. //回复单图文消息
  112. $content = array();
  113. $content[] = array("Title"=>"图文标题1", "Description"=>"图文简述1", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
  114. $content[] = array("Title"=>"图文标题2", "Description"=>"图文简述2", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
  115. $content[] = array("Title"=>"图文标题3", "Description"=>"图文简述3", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url"=>"http://m.cnblogs.com/?u=txw1958");
  116. $result = $this->transmitNews($object, $content);
  117. }
  118. else if ($keyword == "音乐") {
  119. $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇","MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3","HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
  120. $result = $this->transmitMusic($object, $content);
  121. }
  122. return $result;
  123. }
  124. private function receiveImage($object)
  125. {
  126. $content = array("MediaId"=>$object->MediaId);
  127. $result = $this->transmitImage($object, $content);
  128. return $result;
  129. }
  130. private function receiveVoice($object)
  131. {
  132. $content = array("MediaId"=>$object->MediaId);
  133. $result = $this->transmitVoice($object, $content);
  134. return $result;
  135. }
  136. private function receiveVideo($object)
  137. {
  138. $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
  139. $result = $this->transmitVideo($object, $content);
  140. return $result;
  141. }
  142. private function receiveLocation($object)
  143. {
  144. $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
  145. $result = $this->transmitText($object, $content);
  146. return $result;
  147. }
  148. private function receiveLink($object)
  149. {
  150. $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
  151. $result = $this->transmitText($object, $content);
  152. return $result;
  153. }
  154. private function receiveEvent($object)
  155. {
  156. $content = "";
  157. switch ($object->Event) {
  158. case ‘subscribe‘:
  159. $content = "欢迎关注itwo123,科学上网";
  160. break;
  161. case ‘unsubscribe‘:
  162. $content = "";
  163. break;
  164. default:
  165. $content = "";
  166. break;
  167. }
  168. $result = $this->transmitText($object, $content);
  169. return $result;
  170. }
  171. private function receiveMsgType($object)
  172. {
  173. $content = "抱歉,系统故障。"."\n故障代码:".$object->MsgType;
  174. $result = $this->transmitText($object, $content);
  175. return $result;
  176. }
  177. /**
  178. * 文本形式回复用户发送的消息
  179. */
  180. private function transmitText($object, $content)
  181. {
  182. $textTpl = "<xml>
  183. <ToUserName><![CDATA[%s]]></ToUserName>
  184. <FromUserName><![CDATA[%s]]></FromUserName>
  185. <CreateTime><![CDATA[%s]]></CreateTime>
  186. <MsgType><![CDATA[text]]></MsgType>
  187. <Content><![CDATA[%s]]></Content>
  188. </xml>
  189. ";
  190. $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
  191. return $result;
  192. }
  193. /**
  194. * 回复图片消息
  195. */
  196. private function transmitImage($object, $imageArray)
  197. {
  198. $itemTpl = "<Image>
  199. <MediaId><![CDATA[%s]]></MediaId>
  200. </Image>";
  201. $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]);
  202. $textTpl = "<xml>
  203. <ToUserName><![CDATA[%s]]></ToUserName>
  204. <FromUserName><![CDATA[%s]]></FromUserName>
  205. <CreateTime><![CDATA[%s]]></CreateTime>
  206. <MsgType><![CDATA[image]]></MsgType>
  207. $item_str
  208. </xml>";
  209. $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  210. return $result;
  211. }
  212. /**
  213. * 回复语音消息
  214. */
  215. private function transmitVoice($object, $voiceArray)
  216. {
  217. $itemTpl = "<Voice>
  218. <MediaId><![CDATA[%s]]></MediaId>
  219. </Voice>";
  220. $item_str = sprintf($itemTpl, $voiceArray[‘MediaId‘]);
  221. $textTpl = "<xml>
  222. <ToUserName><![CDATA[%s]]></ToUserName>
  223. <FromUserName><![CDATA[%s]]></FromUserName>
  224. <CreateTime><![CDATA[%s]]></CreateTime>
  225. <MsgType><![CDATA[voice]]></MsgType>
  226. $item_str
  227. </xml>";
  228. $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  229. return $result;
  230. }
  231. /**
  232. * 回复视频消息
  233. */
  234. private function transmitVideo($object, $VideoArray)
  235. {
  236. $itemTpl = "<Video>
  237. <MediaId><![CDATA[%s]]></MediaId>
  238. <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
  239. <Title><![CDATA[%s]]></Title>
  240. <Description><![CDATA[%s]]></Description>
  241. </Video>";
  242. $item_str = sprintf($itemTpl, $VideoArray[‘MediaId‘], $VideoArray[‘ThumbMediaId‘], $VideoArray[‘Title‘], $VideoArray[‘Description‘]);
  243. $textTpl = "<xml>
  244. <ToUserName><![CDATA[%s]]></ToUserName>
  245. <FromUserName><![CDATA[%s]]></FromUserName>
  246. <CreateTime><![CDATA[%s]]></CreateTime>
  247. <MsgType><![CDATA[video]]></MsgType>
  248. $item_str
  249. </xml>";
  250. $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  251. return $result;
  252. }
  253. /**
  254. * 回复图文信息
  255. */
  256. private function transmitNews($object, $arr_item)
  257. {
  258. if(!is_array($arr_item)){
  259. return;
  260. }
  261. $itemTpl = "<item>
  262. <Title><![CDATA[%s]]></Title>
  263. <Description><![CDATA[%s]]></Description>
  264. <PicUrl><![CDATA[%s]]></PicUrl>
  265. <Url><![CDATA[%s]]></Url>
  266. </item>";
  267. $item_str = "";
  268. foreach ($arr_item as $item) {
  269. $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);
  270. }
  271. $newsTpl = "<xml>
  272. <ToUserName><![CDATA[%s]]></ToUserName>
  273. <FromUserName><![CDATA[%s]]></FromUserName>
  274. <CreateTime>%s</CreateTime>
  275. <MsgType><![CDATA[news]]></MsgType>
  276. <Content><![CDATA[]]></Content>
  277. <ArticleCount>%s</ArticleCount>
  278. <Articles>
  279. $item_str
  280. </Articles>
  281. </xml>";
  282. $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
  283. return $result;
  284. }
  285. /**
  286. * 回复音乐消息
  287. */
  288. private function transmitMusic($object, $musicArray)
  289. {
  290. $itemTpl = "<Music>
  291. <Title><![CDATA[%s]]></Title>
  292. <Description><![CDATA[%s]]></Description>
  293. <MusicUrl><![CDATA[%s]]></MusicUrl>
  294. <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
  295. </Music>";
  296. $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]);
  297. $textTpl = "<xml>
  298. <ToUserName><![CDATA[%s]]></ToUserName>
  299. <FromUserName><![CDATA[%s]]></FromUserName>
  300. <CreateTime>%s</CreateTime>
  301. <MsgType><![CDATA[music]]></MsgType>
  302. $item_str
  303. </xml>";
  304. $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  305. return $result;
  306. }
  307. }




被动回复用户消息和关注事件推送

标签:

原文地址:http://www.cnblogs.com/itwo/p/5721421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!