tp5
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
public $config;
public function _initialize()
{
$this->config = config('wxapp');
}
public function index()
{
$info = file_get_contents('php://input');
file_put_contents(APP_PATH.'../a.json',$info);
echo 'success';
}
public function getAccessToken()
{
$isExpires = $this->isExpires();
if($isExpires === false){
//到期,获取新的
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->config['appid'] . '&secret=' . $this->config['appsecret'];
$res = $this->curl($url);
// dump($res);
$arr = json_decode($res,true);
if($arr && !isset($arr['errcode'])){
$arr['time'] = time();
file_put_contents(APP_PATH . '../access_token.json', json_encode($arr));
return $arr['access_token'];
}else{
echo 'error on get access_token';die;
}
}else{
return $isExpires;
}
}
public function isExpires(){
if(!file_exists(APP_PATH . '../access_token.json')){
return false;
}
$res = file_get_contents(APP_PATH . '../access_token.json');
$arr = json_decode($res,true);
if($arr && time()<(intval($arr['time'])+intval($arr['expires_in']))){
//未过期
return $arr['access_token'];
}else{
return false;
}
}
public function curl($url)
{
//初始化
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// 执行后不直接打印出来
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
// 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 不从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}
}