1.在根目录下的/Application/Home/Controller文件夹中的创建UrlController.class.php文件
![](https://box.kancloud.cn/e871b12757932b496d3b2be1c21671e1_705x353.png)
2.二维码ticket的获取,在UrlController.class.php文件中书写代码为:
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class UrlController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth类
private $access_token="";//缓存token
public function __construct(){
parent::__construct();//可能内部已经有这个构造方法了,因此加上这个
if(!session('token')){
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret);//初始化WechatAuth类
$WechatAuth=$this->WechatAuth;
$token=$WechatAuth->getAccessToken();
session(array('expire'=>$token['expires_in']));//设置过期时间
session('token',$token['access_token']);//缓存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth类
$this->access_token=$token;//缓存token
}
}
//生成带参数的二维码
public function createCode(){
header('Content-type:text/html;charset=utf-8;');
$WechatAuth=$this->WechatAuth;
$ticket=$WechatAuth->qrcodeCreate('10086');
var_dump($ticket);
}
}
~~~
3.在微信web开发工具中测试结果为
![](https://box.kancloud.cn/5425666abf659aaebceaf6d76f3303cf_1027x459.png)
4.对上述代码稍作更改
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class UrlController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth类
private $access_token="";//缓存token
public function __construct(){
parent::__construct();//可能内部已经有这个构造方法了,因此加上这个
if(!session('token')){
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret);//初始化WechatAuth类
$WechatAuth=$this->WechatAuth;
$token=$WechatAuth->getAccessToken();
session(array('expire'=>$token['expires_in']));//设置过期时间
session('token',$token['access_token']);//缓存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth类
$this->access_token=$token;//缓存token
}
}
//生成带参数的二维码
public function createCode(){
header('Content-type:text/html;charset=utf-8;');
$WechatAuth=$this->WechatAuth;
$ticket=$WechatAuth->qrcodeCreate('10086');
//var_dump($ticket);
//$src=$WechatAuth->showqrcode($ticket['ticket']);//将获取地址放入浏览器中可以直接获得二维码
//var_dump($src);
echo "<img width='70%' src='https://mp.weixin.qq.com/cgi-bin/showqrcode?
ticket=gQEi8jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyaEhhdW9RSmdmYjAxMDAwMG
cwMzAAAgSVn59aAwQAAAAA'>";
}
}
~~~
5.更改内容
![](https://box.kancloud.cn/4508060a5ef054a127c0a5699863d5ec_831x347.png)
6.在微信web开发工具中显示
![](https://box.kancloud.cn/51255ff329b958ff7b5982136d5685a0_1013x478.png)
7.在根目录下的/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);
case "event":
$this ->eventKey($wechat,$data);
}
}
}
//扫描二维码回复消息
private function eventKey($wechat,$data){
if($data['EventKey']=='10086'){
$text="我正在用10086测试,感谢您是要10086服务";
$this->logger("发送信息:\n".$text);
$wechat-> replyText($text);
}
}
//回复文本消息
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);
$m=M('users');
$data['openid']=$users['openid'];
$data['nickname']=$users['nickname'];
$result=$m->add($data);
if($result){
$text="你的openid是:".$users['openid']."\n你的昵称是:".$users['nickname']."\n你的性别是:".$users['sex']."\n你的城市是:"
.$users['city']."\n你所在的国家是:".$users['country']."\n你所在的省份是:".$users['province'];
echo $text;
}
// var_dump($users);
}
}
//日志
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);
}
}
~~~
8.其中的相关代码段
![](https://box.kancloud.cn/12dd2702046deacd30f7f6a89e7ec6cf_608x405.png)
9.扫描第6不步在微信web开发工具中生成的二维码,在微信端就会提示结果信息
![](https://box.kancloud.cn/39761ad9dd292b42814ed8428fe4ba68_404x110.png)
10.本节源码下载(下载密码:7chx)
[源码下载](https://pan.baidu.com/s/1MGTrsmG91edNeYFydnzFlA)