微信服务号开发流程

1.微信公众平台注册账号

2.基本配置里配置服务器

  (1)把这个代码放的需要引用的地方,文档也有说明

    

  1 <?php
  2 /**
  3   * wechat php test
  4   */
  5 
  6 //define your token
  7 define("TOKEN", "chenyi73124");
  8 
  9 class WechatCallbackapi
 10 {
 11 
 12     public function valid()
 13     {
 14         $echoStr = $_GET["echostr"];
 15 
 16         //valid signature , option
 17         if($this->checkSignature()){
 18             ob_clean();
 19             echo $echoStr;
 20             exit;
 21         }
 22     }
 23 
 24     public function responseMsg()
 25     {
 26         //1.获取微信推过来的post数据(xml格式)
 27         $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
 28 
 29         //2.根据消息类型进行回复
 30         if(!empty($postArr)){
 31             $postObj = simplexml_load_string($postArr); //将xml数据转换为对象
 32             $Event= $postObj->Event;//获取时间的类型
 33             //2.1.判断消息类型
 34             if(strtolower($postObj->MsgType) == 'event'){
 35                 //关注事件,新用户关注后自动回复
 36                 if($Event == 'subscribe'){
 37                     $content = '欢迎关注'; //自定义回复内容
 38                     $info = $this->responseText($postObj, $content);
 39                     echo $info;
 40                 } elseif($Event== 'click'){
 41                     //点击菜单事件
 42                     switch($postObj->EventKey){
 43                         case '': //自定义菜单关键字
 44                             $content = ''; //自定义回复内容
 45                             $info = $this->responseText($postObj, $content);
 46                             echo $info;
 47                             break;
 48                         case '': //
 49                             //也可回复图文消息
 50                             $arr = array(
 51                                 array(
 52                                     'title' => '潮图|你的姆爷Slim Shady',
 53                                     'description' => '高能预警',
 54                                     'picUrl' => '',
 55                                     'url' => '',
 56                                 )
 57                             );
 58                             $info = $this->responseNews($postObj, $arr);
 59                             echo $info;
 60                             break;
 61                     }
 62                 } elseif($Event == 'scan') {
 63                     //扫码事件,用户扫码后会跳转至公众号,可自动回复相关内容
 64                     switch ($postObj->EventKey){
 65                         case 2000:
 66                             $content = "";
 67                             $info = $this->responseText($postObj, $content);
 68                             echo $info;
 69                             break;
 70                     }
 71                 }
 72             }
 73             //用户输入关键字回复
 74             if(strtolower($postObj->MsgType) == 'text'){
 75                 $keyword = trim($postObj->Content);
 76                 if(!empty($keyword)) {
 77                     //返回图文信息
 78                     if($keyword == 'slimshady'){
 79                         $arr = array(
 80                             array(
 81                                 'title' => '',
 82                                 'description' => '',
 83                                 'picUrl' => '',
 84                                 'url' => '',
 85                             ),
 86                         );
 87                         $info = $this->responseNews($postObj, $arr);
 88                         echo $info;
 89                     }
 90                     //返回文本消息
 91                     switch ($keyword) {
 92                         case 'shadypop':
 93                             //返回的文本消息也可以是链接形式
 94                             $content = "<a href='http://jason9351.get.vip'>欢迎访问我的主页</a>";
 95                             break;
 96                         default:
 97                             $content = "表示看不懂你说的";
 98                             break;
 99                     }
100                     $info = $this->responseText($postObj, $content);
101                     echo $info;
102                 }
103             }
104         }
105     }
106 
107 //    public function responseText()
108 //    {
109 //        //1.获取微信推过来的post数据(xml格式)
110 //        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
111 //
112 //        //2.根据消息类型进行回复
113 //        if (!empty($postStr)){
114 //
115 //                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //将xml数据转换为对象
116 //                $fromUsername = $postObj->FromUserName;
117 //                $toUsername = $postObj->ToUserName;
118 //                $keyword = trim($postObj->Content);
119 //                $time = time();
120 //                $textTpl = "<xml>
121 //                            <ToUserName><![CDATA[%s]]></ToUserName>
122 //                            <FromUserName><![CDATA[%s]]></FromUserName>
123 //                            <CreateTime>%s</CreateTime>
124 //                            <MsgType><![CDATA[%s]]></MsgType>
125 //                            <Content><![CDATA[%s]]></Content>
126 //                            <FuncFlag>0</FuncFlag>
127 //                            </xml>";
128 //                if(!empty( $keyword ))
129 //                {
130 //                      $msgType = "text";
131 //                    $contentStr = "Welcome to wechat world!";
132 //                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
133 //                    echo $resultStr;
134 //                }else{
135 //                    echo "Input something...";
136 //                }
137 //
138 //        }else {
139 //            echo "";
140 //            exit;
141 //        }
142 //    }
143 
144 //回复文字消息
145     public function responseText($postObj, $content)
146     {
147         $toUser = $postObj->FromUserName;
148         $fromUser = $postObj->ToUserName;
149         $time = time();
150         //返回文字消息的模板
151         $textTpl = "<xml>
152                     <ToUserName><![CDATA[%s]]></ToUserName>
153                     <FromUserName><![CDATA[%s]]></FromUserName>
154                     <CreateTime>%s</CreateTime>
155                     <MsgType><![CDATA[text]]></MsgType>
156                     <Content><![CDATA[%s]]></Content>
157                     </xml>";
158         $info = sprintf($textTpl, $toUser, $fromUser, $time, $content);
159         return $info;
160     }
161 
162 
163     //回复图文消息
164     public function responseNews($postObj, $arr)
165     {
166         $toUser = $postObj->FromUserName;
167         $fromUser = $postObj->ToUserName;
168         $time = time();
169         $newsTpl = "<xml>
170                         <ToUserName><![CDATA[%s]]></ToUserName>
171                         <FromUserName><![CDATA[%s]]></FromUserName>
172                         <CreateTime>%s</CreateTime>
173                         <MsgType><![CDATA[news]]></MsgType>
174                         <ArticleCount>".count($arr)."</ArticleCount>
175                         <Articles>";
176         foreach ($arr as $k => $v) {
177             $newsTpl .= "<item>
178                         <Title><![CDATA[".$v['title']."]]></Title> 
179                         <Description><![CDATA[".$v['description']."]]></Description>
180                         <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
181                         <Url><![CDATA[".$v['url']."]]></Url>
182                         </item>";
183         }
184         $newsTpl .= "</Articles>
185                     </xml>";
186         $info = sprintf($newsTpl, $toUser, $fromUser, $time);
187         return $info;
188     }
189 
190 
191     private function checkSignature()
192     {
193         $signature = $_GET["signature"];
194         $timestamp = $_GET["timestamp"];
195         $nonce = $_GET["nonce"];    
196                 
197         $token = TOKEN;
198         $tmpArr = array($token, $timestamp, $nonce);
199         sort($tmpArr);
200         $tmpStr = implode( $tmpArr );
201         $tmpStr = sha1( $tmpStr );
202         
203         if( $tmpStr == $signature ){
204             return true;
205         }else{
206             return false;
207         }
208     }
209 }
210 
211 ?>

  (2)在控制器写入这串代码

    

 1 //验证微信服务器token
 2     public function index(){
 3 //            header("content-type:text/html;charset=utf-8");
 4         vendor('weichat/WechatCallbackapi');
 5         $wechatObj = new WechatCallbackapi();
 6         if($_GET['echostr'])
 7         {
 8             $wechatObj->valid();
 9         }else
10         {
11             $wechatObj->responseMsg();
12 
13         }
14     }

申明这个就是需要的令牌,

 这个是验证服务器时清楚缓存用的,验证成功过后就可以注释了,好了,现在服务器地址就配好了

 成功过后就是这个样子

3.下一步就是获取全局以及获取用户这些,请看: https://blog.csdn.net/Kit_G/article/details/75084494 ; https://blog.csdn.net/awake720/article/details/79415507 ;http://www.php.cn/php-weizijiaocheng-392976.html

这三个博客应该能帮助你完整的开发服务号了

人生得意须尽欢,莫使金樽空对月.
原文地址:https://www.cnblogs.com/luojie-/p/9290178.html