外部浏览器调用H5调起微信小程序
本次使用的是通过小程序scheme码跳转至小程序
PHP服务端
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Mini extends Controller
{
public $appid = '你的appid';
public $secret = '你的secret';
public function index()
{
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret;
$data = $this->curl_get($url);
$access_token = json_decode($data,true)['access_token'];
$post_data['jump_wxa']['path'] = '/pages/web_view/index';
$post_data['jump_wxa']['query'] = '2';
$post_data = json_encode($post_data);
$post_url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$access_token;
$result = $this->curl_post($post_url,$post_data);
$jumpUrl = json_decode($result,true)['openlink'];
return view('index',[
'openlink'=>$jumpUrl,//跳转路径
]);
}
//curl get方式
public function curl_get($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
//curl post方式
public function curl_post($url, $post_data = '', $timeout = 3000)
{
header("Content-type:text/html;charset=utf-8");
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
}
H5端
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>小程序跳转</title>
<link rel="stylesheet" href="/layui/css/layui.css">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="/layui/layui.js"></script>
</head>
<body>
<input type="button" class="layui-btn" value="打开微信" />
</body>
<script type="text/javascript" charset="utf-8">
$('.layui-btn').click(function(){
window.location.href = '{$openlink}';
})
</script>
</html>