app跳转微信小程序

开发文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html

流程:
1、获取accesstoken。
2、根据文档获取一个 weixin: 开头的链接地址,直接前端 window.localtion.href 跳转即可!

注意事项:
1、accesstoken获取的时间
2、jump_wxa 参数传递的格式要跟文档一致,query只能传递 数字字符格式,有长度限制!

上代码:

private $post_url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=";

    private $appid="要跳转的小程序appid";
    private $appsercet = "要跳转的小程序appsercet";
                          
    private $path ="/pages/index";
    private $env_version ="develop"; //正式版为"release",体验版为"trial",开发版为"develop"

//获取微信返回的跳转地址
    public function get_url(){

        $data = $_POST;
        if(!$data['order_id']){
            return show(0,"信息有误!");
        }
        $access_token = $this->getAsstoken();
     
        $dataa = [
            "jump_wxa" => [
                "path" => $this->path,
                "query" => "跳转到小程序所带的参数(只能传递数字字符)"
            ],
            "expire_time" => 1672502399
        ];
        
        $jump_url = $this->post_url.$access_token;
        $res = $this->sendPOSTRequest($jump_url,json_encode($dataa));
        var_dump($res);
    }

//获取accesstoken
	public function getAsstoken(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsercet;
        $result = file_get_contents($url);
        $res = json_decode($result,true);
        return $res['access_token'];
    }

    public function sendPOSTRequest($url, $map_json)
    {
        $ch = curl_init();
        //设置选项,包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $map_json);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "content-type: application/json; charset=UTF-8"
        ));
        //执行并获取内容
        $content = curl_exec($ch);
        if ($content === false) {
            echo 'Curl error: ' . curl_error($ch);
        }
        //释放curl句柄
        curl_close($ch);
        return $content;

    }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金错刀丨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值