标签 微信小程序 下的文章

    function getScheme(){
    $appid = '你的appkey';
    $secret = '你的appsecret';
    $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
    $token_response = file_get_contents($token_url);
    $token_data = json_decode($token_response, true);
    if (empty($token_data['access_token'])) {
        JsonReturn(['code'=>1,'msg'=>'生成Scheme失败!','data'=>$scheme_result['errmsg']]);
    }
    $access_token = $token_data['access_token'];
    $scheme_data = [
        'jump_wxa' => [
            'path' => '/pages/index/index',//打开页面路径
            'query' => 'from=h5'
        ],
        'expire_type' => 0 //0是1小时有效1永久有效
    ];
    
    $scheme_url = "https://api.weixin.qq.com/wxa/generatescheme?access_token={$access_token}";
    $ch = curl_init($scheme_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($scheme_data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    
    $scheme_response = curl_exec($ch);
    curl_close($ch);
    
    $scheme_result = json_decode($scheme_response, true);
    
    if (empty($scheme_result['openlink'])) {
        //die("生成Scheme失败: " . $scheme_result['errmsg']);
        JsonReturn(['code'=>1,'msg'=>'生成Scheme失败!','data'=>$scheme_result['errmsg']]);
    }
     
    JsonReturn(['code'=>0,'msg'=>'获取成功!','data'=>$scheme_result['openlink']]);
   }