微信公众平台开发

来源:互联网 发布:matlab vb 编辑:程序博客网 时间:2024/04/26 03:05

参考:

http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5  开放平台

http://www.cnblogs.com/mchina/tag/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97/  微信公众平台开发系统

http://sae.sina.com.cn/?m=devcenter&catId=213   新浪服务器

http://www.cnblogs.com/txw1958/category/407134.html  方倍工作室

http://www.bencean.com/

http://www.5icool.org/a/201306/a1908.html    

http://my.csdn.net/gdljw/code/detail/61517



[html] view plaincopy
  1. <?php  
  2. /**  
  3.   * wechat php test  
  4.   */  
  5.   
  6. //define your token  
  7. define("TOKEN", "sheng1990");  
  8. $wechatObj = new wechatCallbackapiTest();  
  9. $wechatObj->responseMsg();  
  10. //$wechatObj->valid();  
  11.   
  12. class wechatCallbackapiTest  
  13. {  
  14.     /*public function valid()  
  15.     {  
  16.         $echoStr = $_GET["echostr"];  
  17.   
  18.         //valid signature , option  
  19.         if($this->checkSignature()){  
  20.             echo $echoStr;  
  21.             exit;  
  22.         }  
  23.     }*/  
  24.   
  25.     public function responseMsg()  
  26.     {  
  27.         //get post data, May be due to the different environments  
  28.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
  29.   
  30.           //extract post data  
  31.         if (!empty($postStr)){  
  32.   
  33.                   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
  34.                 $RX_TYPE = trim($postObj->MsgType);  
  35.   
  36.                 switch($RX_TYPE)  
  37.                 {  
  38.                     case "text":  
  39.                         $resultStr = $this->handleText($postObj);  
  40.                         break;  
  41.                     case "event":  
  42.                         $resultStr = $this->handleEvent($postObj);  
  43.                         break;  
  44.                     default:  
  45.                         $resultStr = "Unknow msg type: ".$RX_TYPE;  
  46.                         break;  
  47.                 }  
  48.                 echo $resultStr;  
  49.         }else {  
  50.             echo "";  
  51.             exit;  
  52.         }  
  53.     }  
  54.   
  55.     public function handleText($postObj)  
  56.     {  
  57.         $fromUsername = $postObj->FromUserName;  
  58.         $toUsername = $postObj->ToUserName;  
  59.         $keyword = trim($postObj->Content);  
  60.         $time = time();  
  61.         //加载文字模板  
  62.         $textTpl = "<xml>  
  63.                     <ToUserName><![CDATA[%s]]></ToUserName>  
  64.                     <FromUserName><![CDATA[%s]]></FromUserName>  
  65.                     <CreateTime>%s</CreateTime>  
  66.                     <MsgType><![CDATA[%s]]></MsgType>  
  67.                     <Content><![CDATA[%s]]></Content>  
  68.                     <FuncFlag>0</FuncFlag>  
  69.                     </xml>";  
  70.   
  71.          //加载图文模版  
  72.          $picTpl = "<xml>  
  73.                     <ToUserName><![CDATA[%s]]></ToUserName>  
  74.                     <FromUserName><![CDATA[%s]]></FromUserName>  
  75.                     <CreateTime>%s</CreateTime>  
  76.                     <MsgType><![CDATA[%s]]></MsgType>  
  77.                     <ArticleCount>1</ArticleCount>  
  78.                     <Articles>          
  79.                     <item>  
  80.                     <Title><![CDATA[%s]]></Title>  
  81.                     <Description><![CDATA[%s]]></Description>  
  82.                     <PicUrl><![CDATA[%s]]></PicUrl>  
  83.                     <Url><![CDATA[%s]]></Url>  
  84.                     </item>                             
  85.                     </Articles>  
  86.                     <FuncFlag>1</FuncFlag>  
  87.                     </xml> ";  
  88.         //加载音乐模板  
  89.          $musicTpl= "<xml>  
  90.                     <ToUserName><![CDATA[%s]]></ToUserName>  
  91.                     <FromUserName><![CDATA[%s]]></FromUserName>  
  92.                     <CreateTime>%s</CreateTime>  
  93.                     <MsgType><![CDATA[%s]]></MsgType>  
  94.                     <Music>  
  95.                     <Title><![CDATA[%s]]></Title>  
  96.                     <Description><![CDATA[%s]]></Description>  
  97.                     <MusicUrl><![CDATA[%s]]></MusicUrl>  
  98.                     <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>  
  99.                     </Music>  
  100.                     </xml>";  
  101.   
  102.   
  103.         if(!empty( $keyword ))  
  104.         {  
  105.             //            $msgType = "text";  
  106.   
  107.             switch($keyword)  
  108.             {  
  109.                 case "m":  
  110.                      $contentStr = "【旭日阳光】\n".  
  111.                           "微信号:SunRise_HY\n".  
  112.                           "万号之家,目前平台功能如下:\n".  
  113.                            "【sh】 生活频道\n".  
  114.                            "【tq】 天气频道\n".  
  115.                            "【ty】 体育频道\n".  
  116.                            "【yl】 电信有礼频道\n".  
  117.                            "【kx】 开心一刻频道\n".  
  118.                            "更多内容,敬请期待...\n";  
  119.                      break;  
  120.                 case "sh":  
  121.                     $contentStr = "欢迎进入生活频道\n"."我们这里包罗万象,请亲输入字母选择对应服务。\n".  
  122.                                   "【hj】 工作环境\n".  
  123.                                   "【jt】 交通车时间\n".  
  124.                                   "【ft】 饭堂\n";  
  125.                     break;  
  126.   
  127.                 case "hj":  
  128.                      $contentStr ="亲,难道您还不知道公司环境的清洁周期吗? 木有怕,其实就是:  
  129.                                     1、楼层地毯每季度清洗一次;  
  130.                                     2、休息室被铺每季度清洗一次;  
  131.                                     3、除四害,每月两次;  
  132.                                     如亲有需要反馈意见,我们就会由专人跟进您反映的问题啦 ,请亲耐心等待。\n";  
  133.                          break;  
  134.   
  135.                 case "tq":  
  136.                     $contentStr = "欢迎进入天气频道\n"."天气晴朗,可放心外出,享受生活。\n";  
  137.                     break;  
  138.                 case "ty":  
  139.                     $contentStr = "欢迎进入体育频道\n"."生命在于运动!亲,我们的活动丰富多彩哦,请输入字母选择对应信息:\n"."【dpw】肚皮舞\n"."【yj】瑜伽社团\n";  
  140.                     break;  
  141.                   
  142.                 case "dpw":  
  143.                     $title = "肚皮舞";  
  144.                     $data  = date('Y-m-d');  
  145.                     $desription = "肚皮舞是一种全身的运动,可以让你的腿部、腹部、肩膀以及颈部都得到充分的活动,从而提高身体的弹性和柔韧性。健身活动可以提升员工身体素质,希望大家积极参与!";  
  146.                     $image = "http://mmsns.qpic.cn/mmsns/oX4NPE2q1ODicOESwiaOJTxIicGu5p8yMhKhhB9fzSa119cegahic38Qwg/0";  
  147.                     $turl = "http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTg1MjYwMQ==&appmsgid=10000123&itemidx=1&sign=9412cd5ce95428bce0154bfe5ac469ce&uin=MTczNTQwMjI2MQ%3D%3D&key=6a68cf733852b46cfca5a13bf752cf102da545c076fb76b49c06bb32525ea8b04a4f810fbce305448142758fdfe01ce2&devicetype=android-16&version=25000104&lang=zh_CN";  
  148.                     break;  
  149.                          
  150.                 case "pic":  
  151.                     $title = "中国电信广东客服";  
  152.                     $data  = date('Y-m-d');  
  153.                     $desription = "中国电信QQ客服是中国电信新型在线服务渠道,为您提供在线业务咨询与办理!QQ用户只需添加QQ号码800010000为好友,就能足不出户轻轻松松办理业务,还有最新优惠信息等着你,快来Q我吧!";  
  154.                     $image = "http://ww1.sinaimg.cn/large/7e274dc0gw1e8plhd17n2j20f0078dhv.jpg";  
  155.                     $turl = "http://e.weibo.com/gdtelkf?type=0";  
  156.                     break;  
  157.                 case "music":  
  158.                     $title = "音乐频道";  
  159.                     $desription = "吻别 演唱:张学友\n"."作词:何启弘 作曲:殷文琦\n" ;  
  160.                     $MusicUrl = "http://music.baidu.com/song/265898";  
  161.                     $HQMusicUrl = "http://music.baidu.com/song/265898";   
  162.                     $msgType = "music";  
  163.                     $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$desription,$MusicUrl,$HQMusicUrl);  
  164.                     echo $resultStr;  
  165.                     break;  
  166.                   
  167.                   
  168.                   
  169.                   
  170.                 default :  
  171.                     $contentStr = "请重新输入正确的指令\n";  
  172.                     break;  
  173.   
  174.             }  
  175.             if($keyword == "pic" || $keyword == "dpw")  
  176.             {  
  177.                 $msgType = "news";  
  178.                 $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);  
  179.                 echo $resultStr;  
  180.             }else  
  181.             {  
  182.                 $msgType = "text";  
  183.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  184.                 echo $resultStr;  
  185.             }  
  186.                      
  187.         }else  
  188.         {  
  189.             echo "Input something...";  
  190.         }  
  191.     }  
  192.   
  193.     public function handleEvent($object)  
  194.     {  
  195.         $contentStr = "";  
  196.         switch ($object->Event)  
  197.         {  
  198.             case "subscribe":  
  199.                $contentStr = "感谢您关注【旭日阳光】\n".  
  200.                           "微信号:SunRise_HY\n".  
  201.                           "万号之家,目前平台功能如下:\n".  
  202.                            "【sh】 生活频道\n".  
  203.                            "【tq】 天气频道\n".  
  204.                            "【ty】 体育频道\n".  
  205.                            "更多内容,敬请期待...\n";  
  206.                 break;  
  207.             default :  
  208.                 $contentStr = "Unknow Event: ".$object->Event;  
  209.                 break;  
  210.         }  
  211.         $resultStr = $this->responseText($object, $contentStr);  
  212.         return $resultStr;  
  213.     }  
  214.   
  215.     public function responseText($object, $content, $flag=0)  
  216.     {  
  217.         $textTpl = "<xml>  
  218.                     <ToUserName><![CDATA[%s]]></ToUserName>  
  219.                     <FromUserName><![CDATA[%s]]></FromUserName>  
  220.                     <CreateTime>%s</CreateTime>  
  221.                     <MsgType><![CDATA[text]]></MsgType>  
  222.                     <Content><![CDATA[%s]]></Content>  
  223.                     <FuncFlag>%d</FuncFlag>  
  224.                     </xml>";  
  225.         $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);  
  226.         return $resultStr;  
  227.     }  
  228.   
  229.   
  230.     private function checkSignature()  
  231.     {  
  232.         $signature = $_GET["signature"];  
  233.         $timestamp = $_GET["timestamp"];  
  234.         $nonce = $_GET["nonce"];  
  235.   
  236.         $token = TOKEN;  
  237.         $tmpArr = array($token, $timestamp, $nonce);  
  238.         sort($tmpArr);  
  239.         $tmpStr = implode( $tmpArr );  
  240.         $tmpStr = sha1( $tmpStr );  
  241.   
  242.         if( $tmpStr == $signature ){  
  243.             return true;  
  244.         }else{  
  245.             return false;  
  246.         }  
  247.     }  
  248. }  
  249.   
  250. ?>  


原创粉丝点击