前言:小编接触php 2年前开始接触了 但是后面转了IOS 。还记得那个时候thinkphp 还是2.3 的版本。现在回头看看 都已经是5.0.3 的版本了。这不最近又拿起来学习一下。才发现很多的结构和用法都变了。这不,小编这个小白就带着大家来研究下这个公众号。
1.首先你需要申请一个微信公众号:
https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=109320610
小编当然会给一个申请流程的连接:
http://jingyan.baidu.com/article/db55b609aaec1e4ba30a2fae.html
2. 然后你需要开启服务号:
小编当然也会当一个连接地址进来了:
http://jingyan.baidu.com/article/e9fb46e190a51a7521f766d7.html
3. 那么我们的微信公众号也开通了。服务号也可以使用了。接下来就是进入开发的阶段了。
>1 接入微信公众号:配置公众号的基本配置。
在这里我们找到:AppID(应用ID)AppSecret(应用密钥)
开发的时候要用到的哦
这里就是我们需要配置开发的地方了哦。
>2 下面我们来看看开发的时候需要如何配置呢:
我这里填写的地址是我的Index 控制器中的index 方法:
//先截图发送下我的代码 然后下面在贴出代码来:
微信公众号对接方法:
设置自定义菜单的函数:
获取token的函数:注意这个token有一个失效时间。所以很多人都是保存到数据库的。
get 请求方法:
获取微信服务器IP地址:
发送消息方法:
下面我来上干货:(代码了)
001 | <?php |
002 | namespace Home\Controller; |
003 | use Think\Controller; |
004 | class IndexController extends Controller { |
005 | |
006 | public function index(){ |
007 | //获得参数 signature nonce token timestamp echostr |
008 | $nonce = $_GET [ 'nonce' ]; |
009 | $token = 'mk_weixin' ; |
010 | $timestamp = $_GET [ 'timestamp' ]; |
011 | $echostr = $_GET [ 'echostr' ]; |
012 | $signature = $_GET [ 'signature' ]; |
013 | //形成数组,然后按字典序排序 |
014 | $array = array (); |
015 | $array = array ( $nonce , $timestamp , $token ); |
016 | sort( $array ); |
017 | //拼接成字符串,sha1加密 ,然后与signature进行校验 |
018 | $str = sha1( implode( $array ) ); |
019 | if ( $str == $signature && $echostr ){ |
020 | //第一次接入weixin api接口的时候 |
021 | // echo $echostr; |
022 | exit ; |
023 | } else { |
024 | $this ->definedItem(); |
025 | $this ->reponseMsg(); |
026 | } |
027 | } |
028 | |
029 | //消息处理函数 |
030 | public function reponseMsg(){ |
031 | //1.获取到微信推送过来post数据(xml格式) |
032 | $postArr = $GLOBALS [ 'HTTP_RAW_POST_DATA' ]; |
033 | //2.处理消息类型,并设置回复类型和内容 |
034 | $postObj = simplexml_load_string( $postArr ); |
035 | |
036 | $access_token = $this ->getWxAccessToken(); |
037 | $fromUser = $postObj ->ToUserName; |
038 | $ToUser = $postObj ->FromUserName; |
039 | $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . '&openid=' . $ToUser ; |
040 | $result = $this ->http_curl( $url , 'get' , 'json' ); |
041 | //存入session |
042 | // session_start(); |
043 | $_SESSION [ 'weixin_openid' ]= $result [ 'openid' ]; |
044 | if ( strtolower ( $postObj ->MsgType) == 'event' ){ |
045 | //如果是关注 subscribe 事件 |
046 | if ( strtolower ( $postObj ->Event == 'subscribe' ) ){ |
047 | //回复用户消息(纯文本格式) |
048 | $toUser = $postObj ->FromUserName; |
049 | $fromUser = $postObj ->ToUserName; |
050 | $time = time(); |
051 | $msgType = 'text' ; |
052 | $content = '欢迎关注我们的微信公众账号' . $postObj ->FromUserName. '-' . $postObj ->ToUserName; |
053 | $template = "<xml> |
054 | <ToUserName><![CDATA[%s]]></ToUserName> |
055 | <FromUserName><![CDATA[%s]]></FromUserName> |
056 | <CreateTime>%s</CreateTime> |
057 | <MsgType><![CDATA[%s]]></MsgType> |
058 | <Content><![CDATA[%s]]></Content> |
059 | </xml>"; |
060 | $info = sprintf( $template , $toUser , $fromUser , $time , $msgType , $content ); |
061 | |
062 | } |
063 | } |
064 | //根据用户输入消息进行回复 |
065 | else if ( strtolower ( $postObj ->MsgType) == 'text' ){ |
066 | $toUser = $postObj ->FromUserName; |
067 | $fromUser = $postObj ->ToUserName; |
068 | $time = time(); |
069 | $msgType = 'text' ; |
070 | //$content = 'imooc is very good'.$postObj->FromUserName.'-'.$postObj->ToUserName; |
071 | $template = "<xml> |
072 | <ToUserName><![CDATA[%s]]></ToUserName> |
073 | <FromUserName><![CDATA[%s]]></FromUserName> |
074 | <CreateTime>%s</CreateTime> |
075 | <MsgType><![CDATA[%s]]></MsgType> |
076 | <Content><![CDATA[%s]]></Content> |
077 | </xml>"; |
078 | switch ( trim( $postObj ->Content)){ |
079 | case 1: |
080 | $content = '您输入的数字是1' ; |
081 | |
082 | break ; |
083 | case 2: |
084 | $content = '您输入的数字是2' ; |
085 | break ; |
086 | case 3: |
087 | $content = '<a href="http://www.tcode.me">淘代码</a>' ; |
088 | break ; |
089 | case tuwen: |
090 | $arr = array ( |
091 | array ( 'title' => 'imooc' , |
092 | 'description' => 'imooc描述' , |
093 | 'picUrl' => 'http://www.tcode.me/static/home/images/logo.png' , |
094 | 'url' => 'http://www.tcode.me' ), |
095 | array ( 'title' => 'hao123' , |
096 | 'description' => 'hao123描述' , |
097 | 'picUrl' => 'http://www.tcode.me/static/home/images/logo.png' , |
098 | 'url' => 'http://www.hao123.com' ), |
099 | array ( 'title' => 'baidu' , |
100 | 'description' => 'baidu描述' , |
101 | 'picUrl' => 'http://www.tcode.me/static/home/images/logo.png' , |
102 | 'url' => 'http://www.tcode.me' ), |
103 | ); |
104 | $content = '<a href="http://www.tcode.me">淘代码</a>' ; |
105 | $template_tuWen = "<xml> |
106 | <ToUserName><![CDATA[%s]]></ToUserName> |
107 | <FromUserName><![CDATA[%s]]></FromUserName> |
108 | <CreateTime>%s</CreateTime> |
109 | <MsgType><![CDATA[%s]]></MsgType> |
110 | <ArticleCount> ".count($arr)." </ArticleCount> |
111 | <Articles>"; |
112 | foreach ( $arr as $k => $v ){ |
113 | $template_tuWen .= "<item> |
114 | <Title><![CDATA[ ".$v['title']." ]]></Title> |
115 | <Description><![CDATA[ ".$v['description']." ]]></Description> |
116 | <PicUrl><![CDATA[ ".$v['picUrl']." ]]></PicUrl> |
117 | <Url><![CDATA[ ".$v['url']." ]]></Url> |
118 | </item>"; |
119 | } |
120 | |
121 | $template_tuWen .="</Articles> |
122 | </xml>"; |
123 | $info = sprintf( $template_tuWen , $toUser , $fromUser , $time , 'news' ); |
124 | // echo $info; |
125 | break ; |
126 | } |
127 | $info = sprintf( $template , $toUser , $fromUser , $time , $msgType , $content ); |
128 | echo $info ; |
129 | } |
130 | } |
131 | |
132 | //$url 接口url string |
133 | //$type 请求类型string |
134 | //$res 返回类型string |
135 | //$arr= 请求参数string |
136 | public function http_curl( $url , $type = 'get' , $res = 'json' , $arr = '' ){ |
137 | |
138 | //1.初始化curl |
139 | $ch =curl_init(); |
140 | //2.设置curl的参数 |
141 | curl_setopt( $ch ,CURLOPT_URL, $url ); |
142 | curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1); |
143 | |
144 | if ( $type == 'post' ){ |
145 | curl_setopt( $ch ,CURLOPT_POST,1); |
146 | curl_setopt( $ch ,CURLOPT_POSTFIELDS, $arr ); |
147 | } |
148 | //3.采集 |
149 | $output =curl_exec( $ch ); |
150 | |
151 | //4.关闭 |
152 | curl_close( $ch ); |
153 | if ( $res == 'json' ){ |
154 | if (curl_error( $ch )){ |
155 | //请求失败,返回错误信息 |
156 | return curl_error( $ch ); |
157 | } else { |
158 | //请求成功,返回错误信息 |
159 | |
160 | return json_decode( $output ,true); |
161 | } |
162 | } |
163 | echo var_dump( $output ); |
164 | } |
165 | |
166 | //获取微信服务器IP地址 |
167 | function getWxServerIp(){ |
168 | $accessToken = $this ->getWxAccessToken(); |
169 | // $accessToken ="Y7cYto0UvJz1U-9NpN04lhQuBkO5BO7Sim6hCZ0GkZlLLfisnvXLjg6VTX_v7veESGX24zAIlu31GD5YXjQFWd5AYXkQTv5a1iGIwk9oxL4gPeWC3fCciWTp2e5ftVZvUXFcAHAHKS"; |
170 | $url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=" . $accessToken ; |
171 | $ch =curl_init(); |
172 | curl_setopt( $ch ,CURLOPT_URL, $url ); |
173 | curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1); |
174 | $res =curl_exec( $ch ); |
175 | //5.关闭curl |
176 | curl_close( $ch ); |
177 | if (curl_error( $ch )){ |
178 | var_dump(curl_error( $ch )); |
179 | } |
180 | $arr =json_decode( $res ,true); |
181 | echo "<pre>" ; |
182 | var_dump( $arr ); |
183 | echo "</pre>" ; |
184 | } |
185 | |
186 | |
187 | //返回access_token *session解决办法 存mysql memcache |
188 | public function getWxAccessToken(){ |
189 | if ( $_SESSION [ 'access_token' ] && $_SESSION [ 'expire_time' ]>time()){ |
190 | //如果access_token在session没有过期 |
191 | echo "111" ; |
192 | // echo $_SESSION['access_token'];; |
193 | return $_SESSION [ 'access_token' ]; |
194 | } |
195 | else { |
196 | //如果access_token比存在或者已经过期,重新取access_token |
197 | //1 请求url地址 |
198 | $AppId = '你的AppId' ; |
199 | $AppSecret = '你的AppSecret' ; |
200 | $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppId . "&secret=" . $AppSecret ; |
201 | $res = $this ->http_curl( $url , 'get' , 'json' ); |
202 | $access_token = $res [ 'access_token' ]; |
203 | //将重新获取到的aceess_token存到session |
204 | $_SESSION [ 'access_token' ]= $access_token ; |
205 | $_SESSION [ 'expire_time' ]=time()+7000; |
206 | return $access_token ; |
207 | } |
208 | } |
209 | |
210 | |
211 | public function definedItem(){ |
212 | //创建微信菜单 |
213 | //目前微信接口的调用方式都是通过 curl post/get |
214 | header( 'content-type:text/html;charset=utf-8' ); |
215 | $access_token = $this ->getWxAccessToken(); |
216 | $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $access_token ; |
217 | $postArr = array ( |
218 | 'button' => array ( |
219 | array ( |
220 | 'name' =>urlencode( '坚果糖果' ), |
221 | // 'type'=>'click', |
222 | 'type' => 'view' , |
223 | // 'key'=>'item1', |
224 | 'url' => 'http://www.tcode.me' |
225 | ), |
226 | array ( |
227 | 'name' =>urlencode( '点外卖' ), |
228 | 'type' => 'view' , |
229 | 'url' => 'http://www.tcode.me' |
230 | // 'sub_button'=>array( |
231 | // array( |
232 | // 'name'=>urlencode('歌曲'), |
233 | // 'type'=>'view', |
234 | // // 'key'=>'songs', |
235 | // 'url'=>'http://www.tcode.me' |
236 | // ),//第一个二级菜单 |
237 | // array( |
238 | // 'name'=>urlencode('电影'), |
239 | // 'type'=>'view', |
240 | // 'url'=>'http://www.tcode.me' |
241 | // ),//第二个二级菜单 |
242 | // ) |
243 | ), |
244 | array ( |
245 | 'name' =>urlencode( '视频游戏' ), |
246 | 'type' => 'view' , |
247 | 'url' => 'http://www.tcode.me' , |
248 | ), //第三个一级菜单 |
249 | |
250 | )); |
251 | echo $postJson = urldecode(json_encode( $postArr )); |
252 | $res = $this ->http_curl( $url , 'post' , 'json' , $postJson ); |
253 | var_dump( $res ); |
254 | } |
255 | |
256 | |
257 | |
258 | public function checkSignature(){ |
259 | $signature = $_GET [ "signature" ]; |
260 | $timestamp = $_GET [ "timestamp" ]; |
261 | $nonce = $_GET [ "nonce" ]; |
262 | echo "获取到的值:" . $signature ; |
263 | $token = “mk_weixin”; |
264 | $tmpArr = array ( $token , $timestamp , $nonce ); |
265 | sort( $tmpArr , SORT_STRING); |
266 | $tmpStr = implode( $tmpArr ); |
267 | $tmpStr = sha1( $tmpStr ); |
268 | |
269 | if ( $tmpStr == $signature ){ |
270 | return true; |
271 | } else { |
272 | return false; |
273 | } |
274 | } |
275 | } |
效果图:
怎么样,你的问题解决了么。解决了就点个赞->就冲下面这个表情。怎么样