自己用thinkphp做微信接口类文件,遇一个问题,GET获取到的token,如何将这个变量传递到实例化的类里面?
<?phpnamespaceWeixin\Controller;useThink\Controller;classIndexControllerextendsContro...
<?php
namespace Weixin\Controller;
use Think\Controller;
class IndexController extends Controller{
public function index(){
$this->token = I('token');
/* 加载微信SDK */
$wechatObj = new wechatCallbackapi();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
}
}
?>
$this->token如何传递给 new wechatCallbackapi();类里面,并且能够使实例化类里的方法调用到这个变量呢? 展开
namespace Weixin\Controller;
use Think\Controller;
class IndexController extends Controller{
public function index(){
$this->token = I('token');
/* 加载微信SDK */
$wechatObj = new wechatCallbackapi();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
}
}
?>
$this->token如何传递给 new wechatCallbackapi();类里面,并且能够使实例化类里的方法调用到这个变量呢? 展开
展开全部
class IndexAction extends Action {
public function index() {
$token = D("wx_token")->field("add_ip", true)->select();
foreach ($token as $key => $v) {
if ($v['expire_time'] - 300 < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$v['appid']}&secret={$v['secret']}";
$res = file_get_contents($url, false);
$this->getAccessToken($v['id'], $res);
}else{
$date = date("Y-m-d H:i:s") ;
echo "{$date}_____no expire\r\n" ;
}
}
}
private function getAccessToken($token, $res) {
$sign = false ;
$json = json_decode($res);
$data['id'] = $token;
$data['access_token'] = $json->access_token;
$data['expire_time'] = time() + 7200;
$data['add_time'] = time();
$data['add_ip'] = '127.0.0.1' ;
$db = D("wx_token");
$db->startTrans();
if ( $db->save($data)){
$db->commit() ;
$sign = true ;
}else{
$db->rollback() ;
$sign = false ;
}
return $sign ;
}
}
再创建个数据表,保存就好了,整个定时任务,定时去获取比较好。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询