H5页面附带参数跳转到小程序

小程序在Onload中接受传递数据【optinos.var】

h5页面请求php鉴权接口获取小程序链接(通过local.hrdf跳转小程序)

前端请求getUrlScheme(附带参数)

public function wx_xcx_code($appid, $appsecret){
                // 获取access_token、有效时间
                $access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
                $access_info = json_decode(file_get_contents($access_url),true);
                return $access_info;
            }

        /**
         * 第二步  拼接接口及请求参数  调用POST请求 返回结果
         */
        public function  getUrlScheme(){
            $access_token = $this->wx_xcx_code("appid", "小程序秘钥);
            $access_token = $access_token['access_token']; // 加个判断,是否获取成功
            $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $access_token;
            $path = '/pages/index/index';

            //query  跳转传递的参数
            $scene = 'phone='.input('phone');
            $post_data = [
                'jump_wxa' => [
                    'path' => $path,
                    'query' => $scene
                ],
                'is_expire' => true,
                'expire_time' => time()+3600
            ];
            $post_data = json_encode($post_data);
            $result = $this->api_notice_increment($url, $post_data);
            $res = $this->object_array(json_decode($result));
            return json($res['openlink']);
        }

        //对象转数组
        public function object_array($array){
            if(is_object($array)) {  
                $array = (array)$array;  
            }
            if(is_array($array)) {
                foreach($array as $key=>$value) {  
                    $array[$key] = $this->object_array($value);  
                }  
            }  
            return $array;
        }
        /**
         * 发起 POST 请求微信接口
         * https://api.weixin.qq.com/wxa/generatescheme?access_token=ACCESS_TOKEN
         * 返回结果
         */
        public function api_notice_increment($url, $data){
            $ch = curl_init();
            $header = ["Accept-Charset" => "utf-8"];
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $tmpInfo = curl_exec($ch);
            // var_dump($tmpInfo);
            if (curl_errno($ch)) {
                return false;
            } else {
                // var_dump($tmpInfo);
                return $tmpInfo;
            }
        }

为您推荐