博客列表 >php H5跳转小程序 最全实例

php H5跳转小程序 最全实例

流
原创
2021年09月17日 11:10:03596浏览
  1. <?php
  2. print_r('<html>'.PHP_EOL);
  3. print_r('<head>'.PHP_EOL);
  4. print_r('</head>'.PHP_EOL);
  5. print_r('<bod>'.PHP_EOL);
  6. print_r('<div>dddddddddddddddddddddd</div>'.PHP_EOL);
  7. //MicroMessenger 是android/iphone版微信所带的
  8. //Windows Phone 是winphone版微信带的 (这个标识会误伤winphone普通浏览器的访问)
  9. $ua = $_SERVER['HTTP_USER_AGENT'];
  10. if(strpos($ua, 'MicroMessenger') == false && strpos($ua, 'Windows Phone') == false){
  11. //weixin://dl/business/?t=mdjw1lxNb7m
  12. //weixin://dl/business/?t=SOqWeEHRu6k
  13. $wxurl = 'weixin://dl/business/?t=SOqWeEHRu6k';
  14. $rest = test();
  15. if($rest['errcode'] == '0'){
  16. $wxurl = $rest['openlink'];
  17. }
  18. $tup = print_r('<script language="javascript" type="text/javascript"> location.href="'.$wxurl.'"; </script> ',1);
  19. print_r('<div>'.print_r($rest,1).'</div>'.PHP_EOL);
  20. print_r('<div>'.$wxurl.'</div>'.PHP_EOL);
  21. echo $tup;
  22. }else{
  23. //微信内
  24. header('location:testurl.html');
  25. }
  26. print_r('</bod>'.PHP_EOL);
  27. print_r('</html>'.PHP_EOL);
  28. function test(){
  29. //探狐小程序 appid wxa************ba2 *******.****.com 4ea*************************7f6e
  30. //tls 小程序 appid wx1************6fb ******.*****.com 690*************************9b12 原始ID:gh_8f**********
  31. $accessToken = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1************6fb&secret=690*************************9b12 ');
  32. $accessToken = json_decode($accessToken,1);
  33. $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$accessToken['access_token'];
  34. $pagesurl = urldecode($_GET['pagesurl']);
  35. $pagesarr = explode('?',$pagesurl);
  36. $path = $pagesarr[0];
  37. if(isset($pagesarr[1])){
  38. $query=$pagesarr[1];
  39. }else{
  40. $query='';
  41. }
  42. var_dump(['path'=>$path,'query'=>$query]);
  43. $data = [];
  44. //pages/task/taskDetail/index
  45. //id=25
  46. //pages/member/myGroup/index
  47. //pages/order/detail/index
  48. //id=694
  49. $data['jump_wxa'] = ['path'=>$path,'query'=>$query];
  50. $data['is_expire'] = true;//到期失效:true,永久有效:false
  51. $data['expire_type'] = 1;//失效时间:0,失效间隔天数:1
  52. //$data['expire_time'] = time()+120;//is_expire 为 true 且 expire_type 为 0 时必填
  53. $data['expire_interval'] = '1';//is_expire 为 true 且 expire_type 为 1 时必填
  54. $data = json_encode($data);
  55. $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
  56. $curl = curl_init();
  57. curl_setopt($curl, CURLOPT_URL, $url);
  58. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  59. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
  60. curl_setopt($curl, CURLOPT_POST, 1);
  61. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  62. curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
  63. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  64. $output = curl_exec($curl);
  65. curl_close($curl);
  66. // print_r(json_decode($output,true)) ;
  67. return json_decode($output,true);
  68. }
  69. ?>

testurl.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0" />
  6. <title>小程序跳转测试</title>
  7. </head>
  8. <body style="text-aligin:center;">
  9. <div class=“test-position”><div>2323</div>
  10. <!-- username="gh_a0*********" gh_8f******** 这里填写小程序的原始ID -->
  11. <wx-open-launch-weapp id="launch-btn" username="gh_8f********" path="packageB/pages/groupDetail/index?goodsid=51&teamid=427&orderid=427">
  12. <!-- /pages/index/index.html这里填写跳转对于小程序的页面 注意这里的 .html -->
  13. <!-- pages/home/index.html?user=123&action=abc这里填写跳转对于小程序的页面 注意这里的 .html -->
  14. <script type="text/wxtag-template">
  15. <style>.btn { padding: 12px }</style>
  16. <button class="btn">打开小程序</button>
  17. </script>
  18. </wx-open-launch-weapp>
  19. </div>
  20. <!--
  21. <script src="/js/jquery-1.12.4.js"></script>-->
  22. <script src="jquery-1.9.1.min.js"></script>
  23. <script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <!-- 至少必须是1.6版本 -->
  24. <script>
  25. $(function () {
  26. // config 的参数以及签名=
  27. var url = location.href.split('#')[0];
  28. $.post("https://tlsmax.tlskoc.com/testurlconfig.php",{url:url}, function(response){
  29. // console.log(response);
  30. var obj = eval("(" + response + ")");
  31. // console.log( obj.status);
  32. if(obj.status == 0) {
  33. var info = obj.data;
  34. // console.log(info);
  35. wx.config({
  36. debug: false,
  37. appId: info.appId,
  38. timestamp: info.timestamp,
  39. nonceStr: info.nonceStr,
  40. signature: info.signature,
  41. 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'
  42. openTagList: ['wx-open-launch-weapp']//必须必须要不调用小程序标签渲染不出来'wx-open-launch-weapp'
  43. });
  44. }
  45. });
  46. //=== 获取 config 的参数以及签名=== end
  47. wx.ready(function () {
  48. //alert('werwre');
  49. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
  50. console.log("res111");
  51. });
  52. wx.error(function (res) {
  53. //alert('sf2323');
  54. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
  55. console.log("res", res);
  56. });
  57. var btn = document.getElementById('launch-btn');
  58. btn.addEventListener('launch', function (e) {
  59. console.log('success');
  60. });
  61. btn.addEventListener('error', function (e) {
  62. console.log('fail', e.detail);
  63. });
  64. btn.addEventListener('WeixinOpenTagsError', function (e) {
  65. console.error(e.detail.errMsg); // 无法使用开放标签的错误原因,需回退兼容。仅无法使用开发标签,JS-SDK其他功能不受影响
  66. });
  67. });
  68. </script>
  69. </body>
  70. </html>

testurl.html

  1. <?php
  2. $res = ['status'=>0,'data'=>actionSqrcode()];
  3. print_r(json_encode($res));
  4. function actionSqrcode()
  5. {
  6. //时间戳
  7. $wx['timestamp'] = time();
  8. //appid
  9. $wx['appId'] ='wxd0****d8*****500';
  10. //生成签名的随机串
  11. $wx['nonceStr'] = md5(time());
  12. //jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。
  13. $wx['jsapi_ticket'] = actionTicket();
  14. //分享的地址,注意:这里是指当前网页的URL,不包含#及其后面部分,曾经的我就在这里被坑了,所以小伙伴们要小心了
  15. $wx['url'] = $_POST['url'];//'https://tlsmax.tlskoc.com/testurl.html';
  16. $string = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", $wx['jsapi_ticket'], $wx['nonceStr'], $wx['timestamp'], $wx['url']);
  17. //生成签名
  18. $wx['signature'] = sha1($string);
  19. return $wx;
  20. }
  21. function actionAccessToken(){
  22. $file = file_get_contents('./token');
  23. $info = json_decode($file,1);
  24. if ($info && $info['time'] > time())
  25. return $info['access_token'];
  26. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd0****d8*****500&secret=502ae58f319cc700f8cf**********f2";
  27. $info = file_get_contents($url);
  28. $info = json_decode($info,1);
  29. if ($info){
  30. $info['time'] = time()+$info['expires_in'];
  31. file_put_contents('./token',json_encode($info));
  32. return $info['access_token'];
  33. }else{
  34. return '失败';
  35. }
  36. }
  37. function actionTicket()
  38. {
  39. $file = file_get_contents('./ticket');
  40. $info = json_decode($file,1);
  41. if ($info && $info['time'] > time())
  42. return $info['ticket'];
  43. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".actionAccessToken()."&type=jsapi";
  44. $info = file_get_contents($url);
  45. $info = json_decode($info,1);
  46. if ($info){
  47. $info['time'] = time()+$info['expires_in'];
  48. file_put_contents('./ticket',json_encode($info));
  49. return $info['ticket'];
  50. }else{
  51. return '失败';
  52. }
  53. }
  54. function actionCurl($url,$data,$type = 'json'){
  55. if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
  56. $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
  57. $data=json_encode($data);
  58. }
  59. $curl = curl_init();
  60. curl_setopt($curl, CURLOPT_URL, $url);
  61. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  62. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  63. if (!empty($data)){
  64. curl_setopt($curl, CURLOPT_POST, 1);
  65. curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
  66. }
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  68. //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
  69. $output = curl_exec($curl);
  70. curl_close($curl);
  71. return $output;
  72. }
  73. ?>

testurlconfig.php

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议