禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。

wordpress小程序教程之如何获取带参数的不限制的小程序码

禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。

在wordpress中获取带参数的小程序码,可以使用微信官方提供的API—wxacode.getUnlimited

代码示例:

// 获取小程序二维码图片
function get_wechat_img($appid, $appSecret, $pid){
// 获取account_token
$access_token = get_wechat_account_token($appid, $appSecret);
if(!$access_token){
return false;
}
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
$data['page'] = 'pages/single/single';
$data['scene'] = $pid;//小程序页面里要接收scene这个参数
$data['width'] = 280;
$data['env_version'] = 'release'; // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
$data['is_hyaline'] = true; // 默认是false,是否需要透明底色,为 true 时,生成透明底色的小程序
//var_dump($data);

$result = curlPost($url,$data,'POST');
$path = './wp-content/uploads/mini';
if(!file_exists($path)){
mkdir($path, 0755, true);
}
$file_name = $pid.'.png';
$ret = file_put_contents( $path.'/'.$file_name, $result, true);
if($ret){
// 写入数据表
$file= home_url().'/wp-content/uploads/mini/'.$file_name;
//update_post_meta($pid,'mini-program',$file);
return $file;
}else{
// var_dump($ret);
return false;
}

}
function curlPost($url,$data,$method){
$ch = curl_init(); //1.初始化 
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 
//4.参数如下 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容 
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 

if($method=="POST"){//5.post方式的时候添加数据 
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
} 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$tmpInfo = curl_exec($ch);//6.执行 

if (curl_errno($ch)) {//7.如果出错 
return curl_error($ch); 
} 
curl_close($ch);//8.关闭 
return $tmpInfo; 
} 
// 获取微信小程序登录access_token
function get_wechat_account_token($appid, $appSecret){

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appSecret;
$res = json_decode(curlPost($url, '', 'GET'), true);

if($res){
// 做缓存处理
return $res['access_token'];
}else{
return false;
}
}

0 个评论

 
联系
目前暂停一切业务,客服消息可能不会及时回复,谢谢理解。
在线咨询
    在线咨询
    禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。