-
先获取ACCESS_TOKEN
$APPID = ‘wx1b110c8123dbbb4’;
$APPSECRET = ‘6305e123f123c231287285c9’;//获取access_token
$access_token = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET”;$json = $this->httpRequest( $access_token );
$json = json_decode( $json, true );
$ACCESS_TOKEN = $json[‘access_token’];
-
在获取二维码
$url = “https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=” . $ACCESS_TOKEN;
$data[‘scene’] = “id=123&page=1”; // 这里需要注意,这里可以用 ‘id=12’ 的格式传递多个参数,最多传递32个字符
$data[‘page’] = ‘pages/goods/goods’;
$data[‘width’] = ‘100’;
$result = $this->httpRequest( $url,json_encode( $data ), ‘POST’ );// 注意 httpRequest 是外部的方法
-
新增 httpRequest 外部方法,发送post请求
function httpRequest( $url, $data = ”, $method = ‘GET’ ) {
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $curl, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT’] );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $curl, CURLOPT_AUTOREFERER, 1 );
if ( $method == ‘POST’ ){
curl_setopt( $curl, CURLOPT_POST, 1 );
if ( $data != ” ){
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
}
}
curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
curl_setopt( $curl, CURLOPT_HEADER, 0 );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $curl );
curl_close( $curl );
return $result;
}
-
前端如何获取 scene
onLoad: function (options) {
if (this.getUrlParam(“cabinet_id”, decodeURIComponent(options.scene))) {
this.setData({ cabinet_id: this.getUrlParam(“cabinet_id”, decodeURIComponent(options.scene)) });//将id赋值给data里面的变量
}
},
// 获取扫码后的字符串
getUrlParam(name, str) {
let regs = new RegExp(“scene=”, “g”); //场景值
str = str.replace(regs, “”);
var reg = new RegExp(“(^|&)” + name + “=([^&]*)(&|$)”);
var array = str.match(reg);
if (array != null)
return array[2];
}, -
如何本地调试
将后端生成小程序保存到本地,通过小程序开发工具打开二维码,进行本地调制
欢迎加我微信: wanbeishijie
一起交流学习或者合作开发