您现在的位置是:网站首页> 编程资料编程资料

php获取小程序码的实现代码(B类接口)_php实例_

2023-05-25 169人已围观

简介 php获取小程序码的实现代码(B类接口)_php实例_

效果图

生成小程序码的php代码

 public function qrcode(){ $member_id = session('id'); if(empty($member_id)) $this->error('请先登录'); //推广二维码 $member = model('Member')->where('id',$member_id)->find(); if($member['is_share'] && $member['share_qrcode']){ $litpic = $member['share_qrcode']; }else{ header('content-type:image/jpg');//加载速度快 // 生成小程序码 $wechatObj = new \Wechat();//这是个类 这里有小程序appid和密码 $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$wechatObj->getAccessToken(); $page = 'pages/index/index'; $scene = 'share_id='.$member_id; $path = './upload/qrcode/'.$member_id.'.jpg'; $postData = array(); $postData['page'] = $page; $postData['scene'] = $scene; $data = json_encode($postData); $result = $this->api_notice_increment($url,$data); $image = 'data:image/jpg;base64,' . base64_encode($result); $other_result = $result; $file = fopen($path,"w");//打开文件准备写入 fwrite($file,$other_result);//写入 fclose($file);//关闭 //return $result; $litpic = $path; $litpic = ltrim($litpic,'.'); //写入数据库 $member->save(['share_qrcode'=>$litpic,'is_share'=>1]); } //推广人数 $path_member = model('Member')->where('path',$member_id)->field('id,name,litpic,add_time')->select(); $path = []; foreach($path_member as $v){ $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); $path[] = $v; } $data = [ 'litpic' => $litpic, 'path' => $path, ]; return json($data); } public function api_notice_increment($url,$data){ //return $data; $curl = curl_init(); // 启动一个CURL会话 //$header = "Accept-Charset: utf-8"; curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检测 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl); } curl_close($curl); // 关键CURL会话 return $tmpInfo; // 返回数据 } function api_notice_increment($url,$data) { $curl = curl_init(); $a = strlen($data); $header = array("Content-Type: application/json; charset=utf-8","Content-Length: $a"); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); curl_close($curl); return $res; }

小程序端获取二维码中带的参数

 /** * 生命周期函数--监听页面加载 */ onLoad: function(option) { console.log(option) if(option.id){ this.setData({ id: option.id }); this.data.business_id = option.id; this.loadData(option.id); } //接受二维码扫码并获取二维码中的参数 if (option.scene){ const ids = decodeURIComponent(option.scene).split('=')[1]; console.log("ids", ids); this.setData({ id: ids }); this.data.business_id = ids; this.loadData(ids); } },

补充上wechat类

wechatId = 1; $this->wechatAppid = 'wx1161dbcdd18c52c2'; $this->wechatAppsecret = 'f373410716a198feb462182c69facb8a'; $this->wechatMchid = 1493574822; $this->wechatPrivatekey = md5(123); */ //客户appid $this->wechatId = 1; $this->wechatAppid = 'your appid'; $this->wechatAppsecret = 'your appsecret'; $this->wechatMchid = 商户号; $this->wechatPrivatekey = '私钥'; /* $this->wechatToken = $WechatInfo['wechat_token']; $this->wechatAccessToken = $WechatInfo['wechat_access_token']; $this->wechatAccessTokenTime = $WechatInfo['wechat_access_token_time']; $this->wechatJsapiTicket = $WechatInfo['wechat_jsapi_ticket']; $this->wechatJsapiTicketTime = $WechatInfo['wechat_jsapi_ticket_time']; */ } // +---------------------------------------------------------------------- // | 获取access_token // +---------------------------------------------------------------------- public function getAccessToken(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->wechatAppid.'&secret='.$this->wechatAppsecret; $data = $this -> curlGet($url); $access_token = $data['access_token']; $expires_in = $data['expires_in']; $save['wechat_access_token'] = $access_token; $save['wechat_access_token_time'] = ($expires_in+time())-360; $this -> wechatAccessToken = $save['wechat_access_token']; $this -> wechatAccessTokenTime = $save['wechat_access_token_time']; return $access_token; } // +---------------------------------------------------------------------- // | 获取access_token // +---------------------------------------------------------------------- public function getJsapiTicket(){ $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this -> getAccessToken().'&type=jsapi'; $data = $this -> curlGet($url); $jsapi_ticket = $data['ticket']; $expires_in = $data['expires_in']; $save['wechat_jsapi_ticket'] = $jsapi_ticket; $save['wechat_jsapi_ticket_time'] = ($expires_in+time())-360; $this->wechatJsapiTicket = $save['wechat_jsapi_ticket']; $this->wechatJsapiTicketTime = $save['wechat_jsapi_ticket_time']; return $jsapi_ticket; } // +---------------------------------------------------------------------- // | 获取signature // +---------------------------------------------------------------------- public function getSignature($appId,$timestamp,$nonceStr,$url) { $jsapi_ticket = $this -> getJsapiTicket(); $string1 = "jsapi_ticket={$jsapi_ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}"; $signature = sha1($string1); return $signature; } // +---------------------------------------------------------------------- // | 获取createNonceStr // +---------------------------------------------------------------------- public function getCreateNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } // +---------------------------------------------------------------------- // | 下载本地 // +---------------------------------------------------------------------- public function curlDownload($url,$name) { $ch = curl_init (); curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt ( $ch, CURLOPT_URL, $url ); ob_start (); curl_exec ( $ch ); $return_content = ob_get_contents (); ob_end_clean (); $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); $filename = "Uploads/Card/{$name}"; $fp= @fopen($filename,"a"); fwrite($fp,$return_content); // 关闭URL请求 curl_close($ch); $url = "/Uploads/Card/{$name}"; return "{$url}"; } // +---------------------------------------------------------------------- // | GET请求 // +---------------------------------------------------------------------- public function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); return $jsoninfo; } // +---------------------------------------------------------------------- // | POST SSL请求 // +---------------------------------------------------------------------- public function curlPostSSL($url, $vars, $second=30,$aHeader=array()){ $ch = curl_init(); //超时时间 curl_setopt($ch,CURLOPT_TIMEOUT,$second); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); //这里设置代理,如果有的话 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/apiclient_cert.pem'); //curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/cert/apiclient_key.pem'); if( count($aHeader) >= 1 ){ curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); } curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); $data = curl_exec($ch); if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } // +---------------------------------------------------------------------- // | 发送退款 // 退款单号 out_trade_no // 交易金额 total_fee // 退款金额 refund_fee // +---------------------------------------------------------------------- public function refund($out_trade_no,$total_fee,$refund_fee){ $arr['appid'] = $this->wechatAppid; $arr['mch_id'] = $this->wechatMchid; $arr['nonce_str'] = $this->getNonceStr(); $arr['out_trade_no'] = $out_trade_no; $arr['out_refund_no'] = $this->getNonceStr(); $arr['total_fee'] = $total_fee; $arr['refund_fee'] = $refund_fee; $arr['sign'] = $this->MakeSign($arr); //将统一下单数组 转换xml $xml = $this->ToXml($arr); //post xml 到微信退款接口 $ur
                
                

-六神源码网