公众号接口文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
关键步骤是通过第三方平台获取access_token
下面是部分代码及步骤
1,
//获取微信第三方平台component_access_token
if(!function_exists('getComponentAccessToken')){
function getComponentAccessToken(){
$cachePath = './cache/componentAccessToken';
$data = json_decode(file_get_contents($cachePath),true);
if($data['expire_time'] < time()){
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
$post = json_encode(array(
'component_appid' => WEIXIN_OPEN_APPID,
'component_appsecret' => WEIXIN_OPEN_APPSECRET,
'component_verify_ticket' => getComponentVerifyTicket(),
));
$res = json_decode(httpPost($url,$post),true);
if($res['component_access_token']){
$data['component_access_token'] = $res['component_access_token'];
$data['expire_time'] = time() + 7000;
file_put_contents($cachePath,json_encode($data));
}
}
return $data['component_access_token'];
}
}
2.
//刷新令牌
if(!function_exists('refreshToken')){
function refreshToken($appid,$token){
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.getComponentAccessToken();
$post = json_encode(array(
'component_appid' => WEIXIN_OPEN_APPID,
'authorizer_appid' => $appid,
'authorizer_refresh_token' => $token,
));
$res = json_decode(httpPost($url,$post),true);
return $res;
}
}
3,
$res = $this->db->query('SELECT accesstoken,expires,refreshtoken FROM ******* WHERE appid = "' . $this->appid . '" LIMIT 1')->row_array();
if($res['refreshtoken']) {
if ($res['expires'] < time()) {
$res = refreshToken($this->appid, $res['refreshtoken']);
if ($res['authorizer_access_token']) {
$res['accesstoken'] = $res['authorizer_access_token'];
$this->ci->db->update('sp_weixin_number', array(
'accesstoken' => $res['authorizer_access_token'],
'expires' => time() + 7000,
), array(
'appid' => $this->appid,
));
}
}
return $res['accesstoken'];
4,
$token = $this->api->token();
$url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$token;
$post = '{"expire_seconds": 7200, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": "'.$_id.'"}}}';
echo httpPost($url, $post);
312