社区新版论坛已上线,点击立即前往!使用 openKylin 账户授权登录,解锁更多体验!

openKylin论坛

 找回密码

微信开发平台攻略函数代码 [复制链接]

   我觉得这篇真的写的好,微信开发不用愁了:http://www.cnblogs.com/txw1958/。下面的代码是精华,其它内容看上面那个链接。
  1. <?php

  2. define("TOKEN", "weixin");

  3. $wechatObj = new wechatCallbackapiTest();
  4. if (!isset($_GET['echostr'])) {
  5.     $wechatObj->responseMsg();
  6. }else{
  7.     $wechatObj->valid();
  8. }

  9. class wechatCallbackapiTest
  10. {
  11.     //验证消息
  12.     public function valid()
  13.     {
  14.         $echoStr = $_GET["echostr"];
  15.         if($this->checkSignature()){
  16.             echo $echoStr;
  17.             exit;
  18.         }
  19.     }

  20.     //检查签名
  21.     private function checkSignature()
  22.     {
  23.         $signature = $_GET["signature"];
  24.         $timestamp = $_GET["timestamp"];
  25.         $nonce = $_GET["nonce"];
  26.         $token = TOKEN;
  27.         $tmpArr = array($token, $timestamp, $nonce);
  28.         sort($tmpArr, SORT_STRING);
  29.         $tmpStr = implode($tmpArr);
  30.         $tmpStr = sha1($tmpStr);

  31.         if($tmpStr == $signature){
  32.             return true;
  33.         }else{
  34.             return false;
  35.         }
  36.     }

  37.     //响应消息
  38.     public function responseMsg()
  39.     {
  40.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  41.         if (!empty($postStr)){
  42.             $this->logger("R ".$postStr);
  43.             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  44.             $RX_TYPE = trim($postObj->MsgType);

  45.             switch ($RX_TYPE)
  46.             {
  47.                 case "event":
  48.                     $result = $this->receiveEvent($postObj);
  49.                     break;
  50.                 case "text":
  51.                     $result = $this->receiveText($postObj);
  52.                     break;
  53.                 case "image":
  54.                     $result = $this->receiveImage($postObj);
  55.                     break;
  56.                 case "location":
  57.                     $result = $this->receiveLocation($postObj);
  58.                     break;
  59.                 case "voice":
  60.                     $result = $this->receiveVoice($postObj);
  61.                     break;
  62.                 case "video":
  63.                     $result = $this->receiveVideo($postObj);
  64.                     break;
  65.                 case "link":
  66.                     $result = $this->receiveLink($postObj);
  67.                     break;
  68.                 default:
  69.                     $result = "unknown msg type: ".$RX_TYPE;
  70.                     break;
  71.             }
  72.             $this->logger("T ".$result);
  73.             echo $result;
  74.         }else {
  75.             echo "";
  76.             exit;
  77.         }
  78.     }

  79.     //接收事件消息
  80.     private function receiveEvent($object)
  81.     {
  82.         $content = "";
  83.         switch ($object->Event)
  84.         {
  85.             case "subscribe":
  86.                 $content = "欢迎关注方倍工作室 ";
  87.                 $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
  88.                 break;
  89.             case "unsubscribe":
  90.                 $content = "取消关注";
  91.                 break;
  92.             case "SCAN":
  93.                 $content = "扫描场景 ".$object->EventKey;
  94.                 break;
  95.             case "CLICK":
  96.                 switch ($object->EventKey)
  97.                 {
  98.                     case "COMPANY":
  99.                         $content = "方倍工作室提供互联网相关产品与服务。";
  100.                         break;
  101.                     default:
  102.                         $content = "点击菜单:".$object->EventKey;
  103.                         break;
  104.                 }
  105.                 break;
  106.             case "LOCATION":
  107.                 $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
  108.                 break;
  109.             case "VIEW":
  110.                 $content = "跳转链接 ".$object->EventKey;
  111.                 break;
  112.             default:
  113.                 $content = "receive a new event: ".$object->Event;
  114.                 break;
  115.         }
  116.         $result = $this->transmitText($object, $content);
  117.         return $result;
  118.     }

  119.     //接收文本消息
  120.     private function receiveText($object)
  121.     {
  122.         switch ($object->Content)
  123.         {
  124.             case "文本":
  125.                 $content = "这是个文本消息";
  126.                 break;
  127.             case "图文":
  128.             case "单图文":
  129.                 $content = array();
  130.                 $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
  131.                 break;
  132.             case "多图文":
  133.                 $content = array();
  134.                 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
  135.                 $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
  136.                 $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
  137.                 break;
  138.             case "音乐":
  139.                 $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
  140.                 break;
  141.             default:
  142.                 $content = date("Y-m-d H:i:s",time());
  143.                 break;
  144.         }
  145.         if(is_array($content)){
  146.             if (isset($content[0]['PicUrl'])){
  147.                 $result = $this->transmitNews($object, $content);
  148.             }else if (isset($content['MusicUrl'])){
  149.                 $result = $this->transmitMusic($object, $content);
  150.             }
  151.         }else{
  152.             $result = $this->transmitText($object, $content);
  153.         }
  154.         return $result;
  155.     }

  156.     //接收图片消息
  157.     private function receiveImage($object)
  158.     {
  159.         $content = array("MediaId"=>$object->MediaId);
  160.         $result = $this->transmitImage($object, $content);
  161.         return $result;
  162.     }

  163.     //接收位置消息
  164.     private function receiveLocation($object)
  165.     {
  166.         $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
  167.         $result = $this->transmitText($object, $content);
  168.         return $result;
  169.     }

  170.     //接收语音消息
  171.     private function receiveVoice($object)
  172.     {
  173.         if (isset($object->Recognition) && !empty($object->Recognition)){
  174.             $content = "你刚才说的是:".$object->Recognition;
  175.             $result = $this->transmitText($object, $content);
  176.         }else{
  177.             $content = array("MediaId"=>$object->MediaId);
  178.             $result = $this->transmitVoice($object, $content);
  179.         }

  180.         return $result;
  181.     }

  182.     //接收视频消息
  183.     private function receiveVideo($object)
  184.     {
  185.         $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
  186.         $result = $this->transmitVideo($object, $content);
  187.         return $result;
  188.     }

  189.     //接收链接消息
  190.     private function receiveLink($object)
  191.     {
  192.         $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
  193.         $result = $this->transmitText($object, $content);
  194.         return $result;
  195.     }

  196.     //回复文本消息
  197.     private function transmitText($object, $content)
  198.     {
  199.         $textTpl = "<xml>
  200. <ToUserName><![CDATA[%s]]></ToUserName>
  201. <FromUserName><![CDATA[%s]]></FromUserName>
  202. <CreateTime>%s</CreateTime>
  203. <MsgType><![CDATA[text]]></MsgType>
  204. <Content><![CDATA[%s]]></Content>
  205. </xml>";
  206.         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
  207.         return $result;
  208.     }

  209.     //回复图片消息
  210.     private function transmitImage($object, $imageArray)
  211.     {
  212.         $itemTpl = "<Image>
  213.     <MediaId><![CDATA[%s]]></MediaId>
  214. </Image>";

  215.         $item_str = sprintf($itemTpl, $imageArray['MediaId']);

  216.         $textTpl = "<xml>
  217. <ToUserName><![CDATA[%s]]></ToUserName>
  218. <FromUserName><![CDATA[%s]]></FromUserName>
  219. <CreateTime>%s</CreateTime>
  220. <MsgType><![CDATA[image]]></MsgType>
  221. $item_str
  222. </xml>";

  223.         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  224.         return $result;
  225.     }

  226.     //回复语音消息
  227.     private function transmitVoice($object, $voiceArray)
  228.     {
  229.         $itemTpl = "<Voice>
  230.     <MediaId><![CDATA[%s]]></MediaId>
  231. </Voice>";

  232.         $item_str = sprintf($itemTpl, $voiceArray['MediaId']);

  233.         $textTpl = "<xml>
  234. <ToUserName><![CDATA[%s]]></ToUserName>
  235. <FromUserName><![CDATA[%s]]></FromUserName>
  236. <CreateTime>%s</CreateTime>
  237. <MsgType><![CDATA[voice]]></MsgType>
  238. $item_str
  239. </xml>";

  240.         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  241.         return $result;
  242.     }

  243.     //回复视频消息
  244.     private function transmitVideo($object, $videoArray)
  245.     {
  246.         $itemTpl = "<Video>
  247.     <MediaId><![CDATA[%s]]></MediaId>
  248.     <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
  249.     <Title><![CDATA[%s]]></Title>
  250.     <Description><![CDATA[%s]]></Description>
  251. </Video>";

  252.         $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);

  253.         $textTpl = "<xml>
  254. <ToUserName><![CDATA[%s]]></ToUserName>
  255. <FromUserName><![CDATA[%s]]></FromUserName>
  256. <CreateTime>%s</CreateTime>
  257. <MsgType><![CDATA[video]]></MsgType>
  258. $item_str
  259. </xml>";

  260.         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  261.         return $result;
  262.     }

  263.     //回复图文消息
  264.     private function transmitNews($object, $newsArray)
  265.     {
  266.         if(!is_array($newsArray)){
  267.             return;
  268.         }
  269.         $itemTpl = "    <item>
  270.         <Title><![CDATA[%s]]></Title>
  271.         <Description><![CDATA[%s]]></Description>
  272.         <PicUrl><![CDATA[%s]]></PicUrl>
  273.         <Url><![CDATA[%s]]></Url>
  274.     </item>
  275. ";
  276.         $item_str = "";
  277.         foreach ($newsArray as $item){
  278.             $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
  279.         }
  280.         $newsTpl = "<xml>
  281. <ToUserName><![CDATA[%s]]></ToUserName>
  282. <FromUserName><![CDATA[%s]]></FromUserName>
  283. <CreateTime>%s</CreateTime>
  284. <MsgType><![CDATA[news]]></MsgType>
  285. <Content><![CDATA[]]></Content>
  286. <ArticleCount>%s</ArticleCount>
  287. <Articles>
  288. $item_str</Articles>
  289. </xml>";

  290.         $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
  291.         return $result;
  292.     }

  293.     //回复音乐消息
  294.     private function transmitMusic($object, $musicArray)
  295.     {
  296.         $itemTpl = "<Music>
  297.     <Title><![CDATA[%s]]></Title>
  298.     <Description><![CDATA[%s]]></Description>
  299.     <MusicUrl><![CDATA[%s]]></MusicUrl>
  300.     <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
  301. </Music>";

  302.         $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

  303.         $textTpl = "<xml>
  304. <ToUserName><![CDATA[%s]]></ToUserName>
  305. <FromUserName><![CDATA[%s]]></FromUserName>
  306. <CreateTime>%s</CreateTime>
  307. <MsgType><![CDATA[music]]></MsgType>
  308. $item_str
  309. </xml>";

  310.         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
  311.         return $result;
  312.     }

  313.     //日志记录
  314.     private function logger($log_content)
  315.     {
  316.         if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
  317.             sae_set_display_errors(false);
  318.             sae_debug($log_content);
  319.             sae_set_display_errors(true);
  320.         }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
  321.             $max_size = 10000;
  322.             $log_filename = "log.xml";
  323.             if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
  324.             file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
  325.         }
  326.     }
  327. }
  328. ?>
复制代码



楼主
发表于 2014-6-30 21:50:50
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

有时间可以学习一下
沙发
发表于 2014-7-1 01:33:27
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

那个网站很强大
板凳
发表于 2014-7-1 09:18:25
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

本帖最后由 lmusic 于 2014-7-1 10:49 编辑

目前微信的可定制性很差,我想调个主题都不允许,都反馈了,没下文了,想要个暗色的主题,不是聊天背景
地板
发表于 2014-7-1 10:48:03
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

开发模式,没试过编辑模式
5#
 楼主| 发表于 2014-7-3 19:27:33
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

joseph 发表于 2014-7-1 01:33
有时间可以学习一下

是的,可以多学学!呵呵
6#
 楼主| 发表于 2014-7-6 15:32:47
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

penghuan 发表于 2014-7-1 09:18
那个网站很强大

哪个网站?你说的是微信公众平台?
7#
 楼主| 发表于 2014-7-6 20:43:39
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

lmy 发表于 2014-7-6 20:43
哪个网站?你说的是微信公众平台?

帖子里面的链接{:3_42:}
8#
发表于 2014-7-7 09:17:50
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

那个网站很强大
9#
发表于 2014-7-7 09:26:16
回复

使用道具 举报

微信开发平台攻略函数代码 [复制链接]

wei6920 发表于 2014-7-7 09:26
那个网站很强大

你也来个网站强大!{:7_141:}
10#
 楼主| 发表于 2014-7-7 20:59:28
回复

使用道具 举报

openKylin

GMT+8, 2024-7-5 03:30 , Processed in 0.030471 second(s), 18 queries , Gzip On.

Copyright ©2022 openKylin. All Rights Reserved .

ICP No. 15002470-12 Tianjin

快速回复 返回顶部 返回列表