/**
*小程序邀请码
**/
public function qrCode() {
// 根据token拿到的用户信息
$userInfo = $this->getUserInfos();
// 做的海报背景图及路径设置,url是小程序想要跳转到的路径,如pages/index/index?refer=;image是背景图;
$slide_item = SlideItemModel::field('url,image')->find();
// 如果已生成直接从数据库读
if($userInfo['qrcode']) {
$this->success('请求成功',[
'qrcode'=>$userInfo['qrcode'],// 二维码
'background' => $slide_item['image'], // 背景图
]);
}
$appid = '你的appid';
$secret = '你的secret';
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$res=$this->http_curl($url);
$qr_url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$res['access_token'];
$postdata = [
"path" => $slide_item['url'].$userInfo['id'],
"width" => 430,
];
$param = json_encode($postdata);
$res = $this->curl_post($qr_url,$param,$options=array());
// 把返回来的二维码路径及图片存储起来
$img = 'upload/qrcode/'.$userInfo['id'].'.jpg';
file_put_contents($img,$res);
$UserModel = UserModel::where('id',$userInfo['id'])->find();
$path= $img;
$UserModel->qrcode = $path;
if($UserModel->save()) {
$this->success('请求成功',[
'qrcode'=>$path,
'background' => $slide_item['image'],
]);
}
}
function http_curl($url) {
//用curl传参
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//关闭ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
function curl_post($url='',$postdata='',$options=array()) {
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($options)) {
curl_setopt_array($ch, $options);
}
$data=curl_exec($ch);
curl_close($ch);
return $data;
}
PHP 获取获取小程序二维码
最新推荐文章于 2024-07-12 10:08:18 发布