生成带参数的小程序码,官方文档详见:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
demo如下:
<?php
getQr();
function getQr(){
$access_token = getAccessToken();
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
$data['scene'] = 'id-13';//此处直接传参数值
$data['page'] = 'pages/articleDetail/articleDetail';//打开的小程序的路径,此处为demo,具体路径和前端确认
$return = curlPost($url,json_encode($data));
//var_dump($return);
$image = burryToBase64($return,'image/png');
echo '<image src='.$image.'></image>';
}
function getAccessToken(){
$grant_type = 'client_credential';
$appid = '你的小程序appid';
$secret = '你的小程序secret';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type='.$grant_type.'&appid='.$appid.'&secret='.$secret;
$accessToken = curlGet($url);
return $accessToken['access_token'];
}
function curlGet($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}
function curlPost($url,$data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
//二进制转图片image/png
function burryToBase64($contents, $mime){
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}