PHP生成微信临时和永久二维码

xiaoxiao2021-02-28  116

<?php class ControllerAccountQrcode extends Controller { //生成微信推广二维码 public function getWechatQrcode() { if (!$this->customer->isLogged()) { $this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL'); $this->response->redirect($this->url->link('account/login', '', 'SSL')); } $this->load->model('account/customer'); //通过openid获取当前代理商ID //$openid=$this->request->get['openid']; $affiliate_id=$this->model_account_customer->getAffiliateIdByCustomerId(); if(!$affiliate_id){ echo "用户未注册成为代理商,无法生成推广二维码";die; } //判断数据里是否已经存在 $qrcodeImg=$this->model_account_customer->getQrodeImg($affiliate_id); if(!empty($qrcodeImg)){ $data['qrcode']=HTTP_SERVER.$qrcodeImg; }else{ //微信基本信息 $appid="***********";//微信appid $secret="****************************";//签名 //获取TOKEN 可用session存储定时请求新的access_token 2小时失效 $access_token=""; $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret; $res = $this->curl_get($token_access_url); //获取文件内容或获取网络请求的内容 //print_r($res);die; $access_token=$res['access_token']; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token; $scene = array( "scene_id" => $affiliate_id ); $action_info = array( "scene" => $scene ); $post_data = array( //"expire_seconds" => "2592000",//当要生成临时二维码时 需要这个参数 "action_name" => "QR_LIMIT_SCENE",//QR_SCENE 临时 QR_LIMIT_SCENE 永久 "action_info" => $action_info ); $post_data=json_encode($post_data); //$post_data='{"expire_seconds":"604800","action_name":"QR_SCENE","action_info":{"scene":{"scene_id":"123"}}}'; $ticket=$this->curl_post($url,$post_data); //根据ticket获取二维码图片 if(isset($ticket['ticket'])){ $url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket['ticket']; $qrcode_img=$this->DownLoadQr($url,time()); if($qrcode_img){ $this->model_account_customer->addQrodeImg($affiliate_id, $qrcode_img); } $data['qrcode']=HTTP_SERVER.$qrcode_img; } } //$data['qrcode']=$this->curl_get($url); //print_r($data['qrcode']); //die; //$data['qrcode'] 这个就是二维码图片地址 下面的代码是分配到模板里面 if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/qrcode.tpl')) { $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/qrcode.tpl', $data)); } else { $this->response->setOutput($this->load->view('default/template/account/qrcode.tpl', $data)); } //print_r($ticket);die; } //Curl get 请求 function curl_get($url){ //初始化curl $ch = curl_init($url); //3.设置参数 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 //4.调用接口 $res = curl_exec($ch); if(curl_errno($ch)){ var_dump(curl_error($ch)); } $resArr = json_decode($res,1); //5.关闭curl curl_close($ch); return $resArr; } //Curl post 请求 function curl_post($url, $post_data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); if(curl_errno($ch)){ var_dump(curl_error($ch)); } $resArr = json_decode($res,1); curl_close($ch); //打印获得的数据 return $resArr; } //将二维码保存到本地 根目录新建 upload/qrcode 这个目录 function DownLoadQr($url,$filestring){ if($url == ""){ return false; } $filename = $filestring.'.jpg'; ob_start(); readfile($url); $img=ob_get_contents(); ob_end_clean(); $size=strlen($img); $fp2=fopen('./upload/qrcode/'.$filename,"a"); if(fwrite($fp2,$img) === false){ return ''; } fclose($fp2); return 'upload/qrcode/'.$filename; } }
转载请注明原文地址: https://www.6miu.com/read-36442.html

最新回复(0)