定时获取微信token

TP5.0框架

<?php
namespace app\shell\command;
 
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Test extends Command
{
    protected $appid= '';
    protected $secret = '';
    protected $template_id = '';
    
	protected function configure()
    {
        parent::configure();
        $this->setName('Test')->setDescription('getToken');
    }
 
    protected function execute(Input $input, Output $output)
    {
        $data=$this->getAccessToken();
        $data=json_decode($data,true);
        $res=Db::name('wtoken')->where('id',1)->update(['token'=>$data['access_token'],'updatetime'=>time()]);
    	exit();
    }
    
    private function getAccessToken()
    {
         //配置appid 
        $appid = $this->appid; 
        //配置appscret 
        $secret = $this->secret; 
        //api接口 
        $api = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; 
        //获取POST请求 
        //发送 
        $str = $this->httpGet($api);
        return $str;
    }
    
    private function httpGet($url){ 
        $curl = curl_init();     
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);     
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);     
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);     
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);     
        curl_setopt($curl, CURLOPT_URL, $url);     
        $res = curl_exec($curl);     
        curl_close($curl);     
        return $res; 
    }
    
}



//linux定时任务
cd /www/wwwroot/项目名
php think Test

执行定时任务前将command路径加入到command.php中