1.在项目根目录下的/Application/Home/Controller文件夹中的IndexController.class.php文件,书写代码
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class IndexController extends Controller {
public function index(){
$token="zhangyouwu";
/* 加载微信 */
$wechat = new Wechat($token);
/* 获取请求信息 */
$data = $wechat->request();
if($data && is_array($data)){
switch($data['MsgType']){
case "text":
$this->Text($wechat,$data);
}
}
}
//回复文本消息
private function Text($wechat,$data){
//回复文本消息
if(strstr($data['Content'],"文本")){
$text="我正在使用Thinkphp开发微信";
$this->logger("发送信息:\n".$text);
$wechat-> replyText($text);
}else if(strstr($data['Content'],"myself")){
$this->users($wechat,$data);
}
}
//获取用户信息
private function users($wechat,$data){
$openid=$data['FromUserName'];
$appid="wx165112bf167af76c";
$appSecret="c65b22bfcf03fdd98504eac299701b03";
$token=session('token');
if($token){
$WechatAuth=new WechatAuth($appid,$appSecret,$token);
}else{
$WechatAuth=new WechatAuth($appid,$appSecret);
$accsseToken=$WechatAuth->getAccessToken();
$token=$accsseToken['access_token'];
session('token',$token);
}
$user=$WechatAuth->userInfo($openid);
$text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n你的性别是:".$user['sex']."\n你的城市是:"
.$user['city']."\n你所在的国家是:".$user['country']."\n你所在的省份是:".$user['province'];
$this->logger("发送用户的信息:\n".$text);
$wechat->replyText($text);
}
//网页授权获取用户基本信息
public function webUsers(){
$appid="wx165112bf167af76c";
$appSecret="c65b22bfcf03fdd98504eac299701b03";
$WechatAuth=new WechatAuth($appid,$appSecret);
if($_GET['iscode']){
$url="http://zyw.gzzysd.com/index.php/Home/Index/webUsers";//zyw.gzzysd.com为微信公众平台的授权回调页面域名
$result=$WechatAuth->getRequestCodeURL($url);
$result;
header("Location:{$result}");//跳转地址
}else if($_GET['code']){
$user=$WechatAuth->getAccessToken('code',$_GET['code']);
var_dump($user);
}
/* $user=$WechatAuth->userInfo($openid);
$text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n你的性别是:".$user['sex']."\n你的城市是:"
.$user['city']."\n你所在的国家是:".$user['country']."\n你所在的省份是:".$user['province'];
$this->logger("发送用户的信息:\n".$text);
$wechat->replyText($text);*/
}
//日志
private function logger($content){//存储日志
$logSize=100000;//定义日志大小
$log="log.txt";//定义日志名字
if(file_exists($log)&&filesize($log)>$logSize){//如果日志存在并且大于定义的大小
unlink($log);//删除
}
//文件写入1.名字2.内容,+时间防止重复3.内容往后加而不是覆盖
file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);
}
}
~~~
在上节新填改代码部分
![](https://box.kancloud.cn/3eb4375cacba2a90abef37a61c966839_515x145.png)
在微信web开发工具中输入地址(http://域名/index.php/Home/Index/webUsers/iscode/1),得到结果:
![](https://box.kancloud.cn/001c7d0d4d526c9082101318c7525db6_1071x577.png)
点击确定登录
![](https://box.kancloud.cn/86e310456d54f14dcfc342e2fc9dfcd1_326x218.png)
再次更改根目录下的/Application/Home/Controller文件夹中的IndexController.class.php文件,书写代码为
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class IndexController extends Controller {
public function index(){
$token="zhangyouwu";
/* 加载微信 */
$wechat = new Wechat($token);
/* 获取请求信息 */
$data = $wechat->request();
if($data && is_array($data)){
switch($data['MsgType']){
case "text":
$this->Text($wechat,$data);
}
}
}
//回复文本消息
private function Text($wechat,$data){
//回复文本消息
if(strstr($data['Content'],"文本")){
$text="我正在使用Thinkphp开发微信";
$this->logger("发送信息:\n".$text);
$wechat-> replyText($text);
}else if(strstr($data['Content'],"myself")){
$this->users($wechat,$data);
}
}
//获取用户信息
private function users($wechat,$data){
$openid=$data['FromUserName'];
$appid="wx165112bf167af76c";
$appSecret="c65b22bfcf03fdd98504eac299701b03";
$token=session('token');
if($token){
$WechatAuth=new WechatAuth($appid,$appSecret,$token);
}else{
$WechatAuth=new WechatAuth($appid,$appSecret);
$accsseToken=$WechatAuth->getAccessToken();
$token=$accsseToken['access_token'];
session('token',$token);
}
$user=$WechatAuth->userInfo($openid);
$text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n你的性别是:".$user['sex']."\n你的城市是:"
.$user['city']."\n你所在的国家是:".$user['country']."\n你所在的省份是:".$user['province'];
$this->logger("发送用户的信息:\n".$text);
$wechat->replyText($text);
}
//网页授权获取用户基本信息
public function webUsers(){
$appid="wx165112bf167af76c";
$appSecret="c65b22bfcf03fdd98504eac299701b03";
$WechatAuth=new WechatAuth($appid,$appSecret);
if($_GET['iscode']){
$url="http://zyw.gzzysd.com/index.php/Home/Index/webUsers";//zyw.gzzysd.com为微信公众平台的授权回调页面域名
$result=$WechatAuth->getRequestCodeURL($url);
$result;
header("Location:{$result}");//跳转地址
}else if($_GET['code']){
header('Content-type:text/html;charset=utf-8');
$user=$WechatAuth->getAccessToken('code',$_GET['code']);
$openid=$user['openid'];//$user是一个数组,直接获取里面的openid
$users=$WechatAuth->getUserInfo($openid);
var_dump($users);
}
/* $user=$WechatAuth->userInfo($openid);
$text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n你的性别是:".$user['sex']."\n你的城市是:"
.$user['city']."\n你所在的国家是:".$user['country']."\n你所在的省份是:".$user['province'];
$this->logger("发送用户的信息:\n".$text);
$wechat->replyText($text);*/
}
//日志
private function logger($content){//存储日志
$logSize=100000;//定义日志大小
$log="log.txt";//定义日志名字
if(file_exists($log)&&filesize($log)>$logSize){//如果日志存在并且大于定义的大小
unlink($log);//删除
}
//文件写入1.名字2.内容,+时间防止重复3.内容往后加而不是覆盖
file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);
}
}
~~~
与上面修改与新添部分代码
![](https://box.kancloud.cn/f714c8c080b53ae353258f0004e32462_572x153.png)
在微信web开发工具输入地址(http://域名/index.php/Home/Index/webUsers/iscode/1),结果为微信的信息:
![](https://box.kancloud.cn/dc6e848a90db9ba0c1b9fc56b061345c_797x330.png)
本节源码下载(下载密码:s2eq)
[源码下载](https://pan.baidu.com/s/16DGd6IyQOb8FxJfRdUNGjw)