php H5 跳转小程序 最全实例

<?php




print_r('<html>'.PHP_EOL);
print_r('<head>'.PHP_EOL);


print_r('</head>'.PHP_EOL);

print_r('<bod>'.PHP_EOL);
print_r('<div>dddddddddddddddddddddd</div>'.PHP_EOL);




//MicroMessenger 是android/iphone版微信所带的
//Windows Phone 是winphone版微信带的 (这个标识会误伤winphone普通浏览器的访问)
$ua = $_SERVER['HTTP_USER_AGENT']; 
if(strpos($ua, 'MicroMessenger') == false && strpos($ua, 'Windows Phone') == false){ 
    //weixin://dl/business/?t=mdjw1lxNb7m
    //weixin://dl/business/?t=SOqWeEHRu6k
    $wxurl = 'weixin://dl/business/?t=SOqWeEHRu6k';
    $rest = test();
    if($rest['errcode'] == '0'){
        $wxurl = $rest['openlink'];
    }
    
    $tup = print_r('<script language="javascript" type="text/javascript"> location.href="'.$wxurl.'"; </script> ',1);
    print_r('<div>'.print_r($rest,1).'</div>'.PHP_EOL);
    print_r('<div>'.$wxurl.'</div>'.PHP_EOL);
    echo $tup; 
}else{ 
    //微信内
    header('location:testurl.html');
}


print_r('</bod>'.PHP_EOL);
print_r('</html>'.PHP_EOL);

function test(){
        //探狐小程序 appid wxa************ba2 	*******.****.com  	4ea*************************7f6e
        //tls 小程序 appid wx1************6fb	******.*****.com   	690*************************9b12   	原始ID:gh_8f**********
        $accessToken = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1************6fb&secret=690*************************9b12 ');
        $accessToken = json_decode($accessToken,1);
        $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$accessToken['access_token'];
        $pagesurl = urldecode($_GET['pagesurl']);
        $pagesarr = explode('?',$pagesurl);
        $path = $pagesarr[0];
        if(isset($pagesarr[1])){
            $query=$pagesarr[1];
        }else{
            $query='';
        }
        
        var_dump(['path'=>$path,'query'=>$query]);
        
        $data = [];
        //pages/task/taskDetail/index
        //id=25
        //pages/member/myGroup/index
        //pages/order/detail/index
        //id=694
        $data['jump_wxa'] = ['path'=>$path,'query'=>$query];
        $data['is_expire'] = true;//到期失效:true,永久有效:false
        $data['expire_type'] = 1;//失效时间:0,失效间隔天数:1
        //$data['expire_time'] = time()+120;//is_expire 为 true 且 expire_type 为 0 时必填
        $data['expire_interval'] = '1';//is_expire 为 true 且 expire_type 为 1 时必填

        $data  = json_encode($data);
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        // print_r(json_decode($output,true)) ;
        return json_decode($output,true);
    }

?>

testurl.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0" />
    <title>小程序跳转测试</title>
</head>
<body style="text-aligin:center;">
<div class=“test-position”><div>2323</div>
    <!-- username="gh_a0*********"    gh_8f******** 这里填写小程序的原始ID -->
<wx-open-launch-weapp id="launch-btn" username="gh_8f********"  path="packageB/pages/groupDetail/index?goodsid=51&teamid=427&orderid=427">
     <!-- /pages/index/index.html这里填写跳转对于小程序的页面 注意这里的 .html -->
     <!-- pages/home/index.html?user=123&action=abc这里填写跳转对于小程序的页面 注意这里的 .html -->
   
    <script type="text/wxtag-template">
        <style>.btn { padding: 12px }</style>
        <button class="btn">打开小程序</button>
    </script>
    
</wx-open-launch-weapp>
</div>

<!--
<script src="/js/jquery-1.12.4.js"></script>-->
<script src="jquery-1.9.1.min.js"></script>

<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <!-- 至少必须是1.6版本 -->

<script>
    
    $(function () {
    
        // config 的参数以及签名=
        var url = location.href.split('#')[0];
        $.post("https://tlsmax.tlskoc.com/testurlconfig.php",{url:url}, function(response){
            // console.log(response);
            var obj = eval("(" + response + ")");
            // console.log( obj.status);
           
            if(obj.status == 0) {
                var info = obj.data;
                // console.log(info);
                wx.config({
                    debug: false,
                    appId: info.appId,
                    timestamp: info.timestamp,
                    nonceStr: info.nonceStr,
                    signature: info.signature,
                    jsApiList: ["startRecord", "stopRecord", "playVoice", "pauseVoice", "stopVoice",  "uploadVoice", "downloadVoice", 'chooseImage',  "uploadImage", "downloadImage", "translateVoice",  "openLocation", "getLocation", 'previewImage'],//必须要不调用小程序标签渲染不出来"startRecord", "stopRecord", "playVoice", "pauseVoice", "stopVoice",  "uploadVoice", "downloadVoice", 'chooseImage',  "uploadImage", "downloadImage", "translateVoice",  "openLocation", "getLocation", 'previewImage'
                    openTagList: ['wx-open-launch-weapp']//必须必须要不调用小程序标签渲染不出来'wx-open-launch-weapp'
                });
            }
        });
        //=== 获取 config 的参数以及签名=== end
        wx.ready(function () {
            //alert('werwre');
            // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
            console.log("res111");
        });

        wx.error(function (res) {
            //alert('sf2323');
            // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
            console.log("res", res);
        });        


        var btn = document.getElementById('launch-btn');
        btn.addEventListener('launch', function (e) {
            console.log('success');
        });
        btn.addEventListener('error', function (e) {
            console.log('fail', e.detail);
        });
        btn.addEventListener('WeixinOpenTagsError', function (e) {
          console.error(e.detail.errMsg); // 无法使用开放标签的错误原因,需回退兼容。仅无法使用开发标签,JS-SDK其他功能不受影响
        });

    });


</script>
</body>
</html>

testurl.html

<?php  


$res = ['status'=>0,'data'=>actionSqrcode()];
print_r(json_encode($res));

function actionSqrcode()
    {
        //时间戳
        $wx['timestamp'] = time();
        //appid
        $wx['appId'] ='wxd0****d8*****500';
        //生成签名的随机串
        $wx['nonceStr'] = md5(time());
        //jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。
        $wx['jsapi_ticket'] = actionTicket();
        //分享的地址,注意:这里是指当前网页的URL,不包含#及其后面部分,曾经的我就在这里被坑了,所以小伙伴们要小心了
        $wx['url'] = $_POST['url'];//'https://tlsmax.tlskoc.com/testurl.html';
        
        $string = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", $wx['jsapi_ticket'], $wx['nonceStr'], $wx['timestamp'], $wx['url']);
        
        //生成签名
        $wx['signature'] = sha1($string);
        return $wx;
    }

function actionAccessToken(){
        $file = file_get_contents('./token');
        $info = json_decode($file,1);
        if ($info && $info['time'] > time())
            return $info['access_token'];

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd0****d8*****500&secret=502ae58f319cc700f8cf**********f2";
        $info = file_get_contents($url);
        $info = json_decode($info,1);
        if ($info){
            $info['time'] = time()+$info['expires_in'];
            file_put_contents('./token',json_encode($info));
            return $info['access_token'];
        }else{
            return '失败';
        }
    }

function actionTicket()
    {
        $file = file_get_contents('./ticket');
        $info = json_decode($file,1);
        if ($info && $info['time'] > time())
            return $info['ticket'];

        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".actionAccessToken()."&type=jsapi";
        $info = file_get_contents($url);
        $info = json_decode($info,1);
        if ($info){
            $info['time'] = time()+$info['expires_in'];
            file_put_contents('./ticket',json_encode($info));
            return $info['ticket'];
        }else{
            return '失败';
        }
    }

function actionCurl($url,$data,$type = 'json'){
        if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
            $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $data=json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


?>

testurlconfig.php

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值