thinkphp 实现微信公众号开发(一)
打算用php做一个微信公众号管理平台。用thinkphp框架
教程上面是是应用下面会自动生成Lib文件夹。但是我的3.2.3自动生成目录不一样
我修改了我项目文件夹下面的IndexController.class.php文件
D:\phpStudy\WWW\weixin\Imooc\Home\Controller\IndexController.class.php
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ $nonce = $_GET[\'nonce\']; $token = \'imooc\'; $timestamp=$_GET[\'timestamp\']; $echostr = $_GET[\'echostr\']; $signature = $_GET[\'signature\']; $array = array($nonce,$timestamp,$token); sort($array); $str = sha1(implode($array)); if($str == $signature && $echostr){ //第一次接入weixin api 接口的时候 echo $echostr; exit; }else{ $this->reponseMsg(); } } public function show(){ echo \'imooc\'; }
}
在浏览器中输入以下地址,可以返回数据
http://localhost/weixin/index.php/Home/Index/show
微信中输入 提交正常
http://www.zzcit.com/weixin/index.php/Home/Index/index
以上证明了微信接口接入正常
下面代码则通过关注或者取消关注对用户返回数据。微信开发之路正式开始
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { /* public function index(){ $this->show(\'<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>\',\'utf-8\'); }*/ public function __construct(){ } public function index(){ //获得参数 signature nonce token timestamp echostr $nonce = $_GET[\'nonce\']; $token = \'imooc\'; $timestamp = $_GET[\'timestamp\']; $echostr = $_GET[\'echostr\']; $signature = $_GET[\'signature\']; //形成数组,然后按字典序排序 $array = array(); $array = array($nonce, $timestamp, $token); sort($array); //拼接成字符串,sha1加密 ,然后与signature进行校验 $str = sha1( implode( $array ) ); if( $str == $signature && $echostr ){ //第一次接入weixin api接口的时候 echo $echostr; exit; }else{ $this->reponseMsg(); } } public function show(){ echo \'imooc\'; } public function reponseMsg(){ //1.获取到微信推送过来post数据(xml格式) $postArr = $GLOBALS[\'HTTP_RAW_POST_DATA\']; //2.处理消息类型,并设置回复类型和内容 /*<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> </xml>*/ $postObj = simplexml_load_string( $postArr ); //$postObj->ToUserName = \'\'; //$postObj->FromUserName = \'\'; //$postObj->CreateTime = \'\'; //$postObj->MsgType = \'\'; //$postObj->Event = \'\'; // gh_e79a177814ed //判断该数据包是否是订阅的事件推送 if( strtolower( $postObj->MsgType) == \'event\'){ //如果是关注 subscribe 事件 if( strtolower($postObj->Event == \'subscribe\') ){ //回复用户消息(纯文本格式) $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = \'text\'; $content = \'欢迎关注我们的微信公众账号\'.$postObj->FromUserName.\'-\'.$postObj->ToUserName; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; /*<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[你好]]></Content> </xml>*/ } } }}