微擎XX模块 后端获取抖音openid代码

XX微擎模块 获取抖音api token代码

function get_this_AccessToken()
{
    global $_W;
    $appid = $_W["account"]["key"];
    $secret = $_W["account"]["secret"];
    $uniacid = $_W["uniacid"];
    $current_access_token_arr = cache_load($uniacid . "_current_access_token");
    if (!empty($current_access_token_arr["access_token"]) && $current_access_token_arr["time"] > time()) {
        $current_access_token = $current_access_token_arr["access_token"];
    } else {
        load()->func("communication");
        $row = ihttp_get("https://developer.toutiao.com/api/apps/v2/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
        $res = json_decode($row["content"], true);
        $current_access_token = $res["access_token"];
        $access_token_arr["time"] = time() + 5000;
        $access_token_arr["access_token"] = $current_access_token;
        cache_write($uniacid . "_current_access_token", $access_token_arr);
    }
    return $current_access_token;
}

XX微擎模块 获取抖音用户openid代码

function get_openid($code)
{
    global $_W;
    $appid = $_W["account"]["key"];
    $secret = $_W["account"]["secret"];
    $url = 'https://developer.toutiao.com/api/apps/v2/jscode2session';
 $code=$_GET["code"];     

   $params = array(
                'appid' => $appid,
                'secret' => $secret,
                'code' => $code,
            );
        $postdata = json_encode($params);
        $opts = array(
            'http'=>array(
                'method'=>'POST',
                'header'=>'Content-Type:application/json',
                'content'=>$postdata,
                )
            );
        $context = stream_context_create($opts);
        $data = file_get_contents($url,false,$context);
        return $code;
}