1.在根目錄下的/Application/Home/Controller文件夾中的創建UrlController.class.php文件

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開發工具中測試結果為

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.更改內容

6.在微信web開發工具中顯示

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.其中的相關代碼段

9.掃描第6不步在微信web開發工具中生成的二維碼,在微信端就會提示結果信息

10.本節源碼下載(下載密碼:7chx)
[源碼下載](https://pan.baidu.com/s/1MGTrsmG91edNeYFydnzFlA)