/**
* 获取小程序全局唯一后台接口调用凭据(access_token)
**/
function getAccess_token(){
$system = $this->wx_system->where("id = 1")->find();
if($system['access_token'] == '' && $system['access_token_expires_in'] == ''){
// 通过微信的code获取用户的openid(用户唯一id)和session_key(会话秘钥)
$result =file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$system['appId'].'&secret='.$system['appSecret']);
echo '内容为空,'.$result.'***';
} else if($system['access_token'] != '' && (intval($system['access_token_expires_in']) + 7199) <= time()){
// 通过微信的code获取用户的openid(用户唯一id)和session_key(会话秘钥)
$result =file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$system['appId'].'&secret='.$system['appSecret']);
echo '小于当前时间,'.$result.'***';
}
$result = !$result?false:json_decode($result, true);
if($result){
echo '赋值了,';
$this->wx_system->where("id = 1")->save(['access_token' => $result['access_token'], 'access_token_expires_in' => time()]);
}
// return $result;
if(!$result){
echo '返回数据的,';
return $system['access_token'];
} else {
echo '返回接口获取的,';
return $result['access_token'];
}
}
/**
* 获取微信统计相关数据
*/
function getWxPost($url,$data){
$options['http'] = array(
'timeout'=>60,
'method' => 'POST',
'header' => 'Content_Type:application/json;charset=utf-8',
'content' => $data
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
// 调用部分
$access_token = $this->tool->getAccess_token();
$visitUser = $this->tool->getWxPost('https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token='.$access_token,['begin_date'=>date('yymd'),'end_date'=>date('yymd')]);
之前以为是access_token的失效问题,现在是一直报44002,第一次接触调用数据这块不是太了解,求大神解答
试看看通过curl调用