微信中定时任务执行时,提示access_token失效(小记)

情景再现:定时任务通了,但定时函数定时执行时,日志中打印出错误信息access_token失效(或获取不到等),测试表中的access_token为0;而自己直接访问定时函数的链接时是可以获取到access_token的。

原因:自己执行时,是在微信中打开的链接,这样执行获取access_token的函数时传递了“使用微信”的token值;而定时函数定时执行时,没有使用微信,即没有token参数传递给获取access_token的函数,这样就获取不到有效的access_token。

解决方法:定时任务使用获取access_token函数时,传递一个长期有效的token参数,如商家联系人的微信token(保证该微信一直关注微信公众号即可)。

相关函数:

// 获取access_token,自动带缓存功能
function get_access_token($token = '', $update = false) {
    empty ( $token ) && $token = get_token ();

    $info = get_token_appinfo ( $token );

    // 微信开放平台一键绑定
    if ($token == 'gh_3c884a361561' || $info ['is_bind']) {
        $access_token = get_authorizer_access_token ( $info ['appid'], $info ['authorizer_refresh_token'], $update );
    } else {
        $access_token = get_access_token_by_apppid ( $info ['appid'], $info ['secret'], $update );
    }

    // 自动判断access_token是否已失效,如失效自动获取新的
    if ($update == false) {
        $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $access_token;
        $res = wp_file_get_contents ( $url );
        $res = json_decode ( $res, true );
        if ($res ['errcode'] == '40001') {
            $access_token = get_access_token ( $token, true );
        }
    }

    return $access_token;
}

// 获取当前用户的Token
function get_token($token = NULL) {
    $stoken = session ( 'token' );

    $reset = false;
    if ($token !== NULL && $token != '-1') {
        session ( 'token', $token );
        $reset = true;
    } elseif (! empty ( $_REQUEST ['token'] ) && $_REQUEST ['token'] != '-1') {
        session ( 'token', $_REQUEST ['token'] );
        $reset = true;
    } elseif (! empty ( $_REQUEST ['publicid'] )) {
        $publicid = I ( 'publicid' );
        $token = D ( 'Common/Public' )->getInfo ( $publicid, 'token' );
        $token && session ( 'token', $token );
        $reset = true;
    }
    $token = session ( 'token' );

    if (! empty ( $token ) && $token != '-1' && $stoken != $token && $GLOBALS ['is_wap']) {
        session ( 'mid', null );
    }
    //加校验,防止使用无权限的公众号
    /*if(!$GLOBALS['is_wap'] && $reset){
        if(empty($GLOBALS['myinfo'])) $token = -1;
        else{
            $sql = 'SELECT public_id FROM `'.C('DB_PREFIX').'public_link` as l LEFT JOIN '.C('DB_PREFIX').'public as p on l.mp_id=p.id WHERE l.uid='.$GLOBALS['mid'];
            $list = M()->query($sql);
            $flat = false;
            foreach ($list as $value) {
                if($value['public_id']==$token){
                    $flat = true;
                }
            }

            if(!$flat) $token = -1;
        }
    }*/

    if (empty ( $token ) ) {
        $token = -1;
    }

    return $token;
}

// 获取公众号的信息
function get_token_appinfo($token = '', $field = '') {
    empty ( $token ) && $token = get_token ();
    if ($token != 'gh_3c884a361561') {
        $info = D ( 'Common/Public' )->getInfoByToken ( $token, $field );
    }
    return $info;
}

function get_authorizer_access_token($appid, $refresh_token, $update) {
    if (empty ( $appid )) {
        return 0;
    }

    $key = 'authorizer_access_token_' . $appid;
    $res = S ( $key );
    if ($res !== false && ! $update)
        return $res;

    $dao = D ( 'Addons://PublicBind/PublicBind' );
    if (empty ( $refresh_token )) {
        $auth_code = $dao->_get_pre_auth_code ();
        $info = $dao->getAuthInfo ( $auth_code );
        $authorizer_access_token = $info ['authorization_info'] ['authorizer_access_token'];
    } else {
        $info = $dao->refreshToken ( $appid, $refresh_token );
        $authorizer_access_token = $info ['authorizer_access_token'];
    }

    if (! empty ( $authorizer_access_token )) {
        S ( $key, $authorizer_access_token, $info ['expires_in'] - 200 );
        return $authorizer_access_token;
    } else {
        addWeixinLog ( $info, 'get_authorizer_access_token_fail_' . $appid );
        return 0;
    }
}
function get_access_token_by_apppid($appid, $secret, $update = false) {
    if (empty ( $appid ) || empty ( $secret )) {
        return 0;
    }

    $key = 'access_token_apppid_' . $appid . '_' . $secret;
    $res = S ( $key );
    if ($res !== false && ! $update)
        return $res;

    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&secret=' . $secret . '&appid=' . $appid;
    $tempArr = json_decode ( get_data ( $url ), true );
    if (@array_key_exists ( 'access_token', $tempArr )) {
        S ( $key, $tempArr ['access_token'], $tempArr ['expires_in'] );
        return $tempArr ['access_token'];
    } else {
        return 0;
    }
}
// 防超时的file_get_contents改造函数
function wp_file_get_contents($url) {
    return get_data ( $url, 30 );
}
// 以GET方式获取数据,替代file_get_contents
function get_data($url, $timeout = 5) {
    $msg = $flat = '';
    if (strpos ( $url, 'http://' ) !== false || strpos ( $url, 'https://' ) !== false) {

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );

        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); // 跳过证书检查
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // 不检查证书
        $res = curl_exec ( $ch );

        $flat = curl_errno ( $ch );
        if ($flat) {
            $msg = curl_error ( $ch );
        }
        curl_close ( $ch );
    } else {
        $context = stream_context_create ( array (
                'http' => array (
                        'timeout' => 30 
                ) 
        ) ); // 超时时间,单位为秒

        $res = file_get_contents ( $url, 0, $context );
    }
    return $res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值