因为在easywechat中没有说起在thinkphp中的使用,后来我在http://www.thinkphp.cn/topic/45416.html中找到了有人已经封装了一下,我把本身使用的过程写下来与你们共勉php
在thinkphp中安装easywechathtml
1.使用composer下载git
使用命令行进入thinkphp根目录github
而后运行下面的命令:thinkphp
而后发布配置文件到项目根目录数据库
而后你会看到application目录下多了一个extra文件夹,里面有一个wechat.php,若是报错了,请参考 https://www.ailoli.org/archives/72/,这样就算是引入成功了api
而后微信
填写配置文件须要填写的项session
示例:app
'debug' => true,
/**
* 帐号基本信息,请从微信公众平台/开放平台获取
*/
'app_id' => '......', // AppID
'secret' => '......', // AppSecret
'token' => '......', // Token
'aes_key' => '',
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '回调地址',
],
而后,在原代码基础上建立一个控制器(与微信相关):Wechat1.php,
在里面定义一个变量app
$options = Config::get('wechat');
$app = new Application($options);
这样就可以使用app变量了,其余的用法参照文档https://www.easywechat.com/docs便可
配置和原来相似,我是在Wechat1.php中定义一个serve方法
public function serve(){
$server = self::$app->server;
$server->setMessageHandler(function ($message) {
return '你好';
});
$server->serve()->send();
}
在微信公众号后台验证token的url写可以访问到这个serve方法的连接便可验证成功
下面重点说明我使用easywechat进行网页受权过程
在须要受权的控制器Personal.php中的写了
static $app;
public function _initialize()
{
if (empty(session('id'))){
self::$app = Wechat1::return_app();
$oauth = self::$app->oauth;
session('target_url',$_SERVER['PATH_INFO']);
if (empty(session('wechat_user'))){
$oauth->redirect()->send();
}else{
$user = session('wechat_user');
$open_id = $user['original']['openid'];
//查询数据库中用户的帐号的openid中是否有值,有值说明用户的微信与帐号绑定
$student_no = self::check_login($open_id);
if ($student_no!=0){
session('id',$student_no);
$this->redirect(session('target_url'));
}else{
$this->redirect('index/Index/login');
}
}
}
}
而后在Wechat1.php中写了一个受权回调的方法
public function oauth(){
$oauth = self::$app->oauth;
$user = $oauth->user();
session('wechat_user',$user->toArray());
$targetUrl = session('target_url');
$this->redirect($targetUrl);
}
注:上面的配置文件中的回调函数就写可以找到oauth方法的地址便可
这样就可以完成微信网页受权,受权过的微信的用户信息存在session中,以后用到该用户信息的时候,只须要从session中取便可
### 注:如今官方网站上面支持thinkphp了,可使用官方支持的扩展包,应该会更方便一些!!!thinkphp-wechat地址