php获取小程序码的实现代码(B类接口)
程序员文章站
2022-07-04 23:31:24
效果图生成小程序码的php代码public function qrcode(){ $member_id = session('id'); if(empty($member_id)) $th...
效果图
生成小程序码的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类
<?php class wechat { // +---------------------------------------------------------------------- // | 参数 // +---------------------------------------------------------------------- public $table; public $where_web; public $wechatid; public $wechattoken; public $wechatappid; public $wechatappsecret; public $wechatmchid; public $wechatprivatekey; public $wechataccesstoken; public $wechataccesstokentime; public $wechatjsapiticket; public $wechatjsapitickettime; // +---------------------------------------------------------------------- // | 自动加载 // +---------------------------------------------------------------------- public function __construct() { //测试 /*$this->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 到微信退款接口 $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";;//微信退款地址,post请求 $ch=curl_init(); //需要获取的url地址,也可以在 curl_init() 函数中设置。 curl_setopt($ch,curlopt_url,$url); //启用时会将头文件的信息作为数据流输出。 //curl_setopt($ch,curlopt_header,1); //将 curl_exec() 获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($ch,curlopt_returntransfer,1); //证书检查 curl_setopt($ch,curlopt_ssl_verifypeer,false); //证书的类型。支持的格式有"pem" (默认值), "der"和"eng"。 curl_setopt($ch,curlopt_sslcerttype,'pem'); //一个包含pem格式证书的文件名。 curl_setopt($ch,curlopt_sslcert,gen.'/cert/apiclient_cert.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_timeout,30); //包含ssl私钥的文件名。 curl_setopt($ch,curlopt_sslkey,gen.'/cert/apiclient_key.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); //一个保存着1个或多个用来让服务端验证的证书的文件名。这个参数仅仅在和 curlopt_ssl_verifypeer 一起使用时才有意义。 . // curl_setopt($ch,curlopt_cainfo,getcwd().'/cert/rootca.pem'); curl_setopt($ch,curlopt_post,1); curl_setopt($ch,curlopt_postfields,$xml); $data=curl_exec($ch); if($data){ curl_close($ch); $data_arr = json_decode(json_encode(simplexml_load_string($data, 'simplexmlelement', libxml_nocdata)), true); return $data_arr; }else{ $error = curl_errno($ch); return "curl 错误:".$error; } } // +---------------------------------------------------------------------- // | 企业付款 // 退款单号 out_trade_no // 交易金额 total_fee // 退款金额 refund_fee // +---------------------------------------------------------------------- public function payment($partner_trade_no,$openid,$amount,$desc){ // 获取 $arr['mch_appid'] = $this->wechatappid; $arr['mchid'] = $this->wechatmchid; $arr['nonce_str'] = $this->getnoncestr(); $arr['partner_trade_no'] = $partner_trade_no; $arr['openid'] = $openid; $arr['check_name'] = "no_check"; $arr['amount'] = $amount*100; $arr['desc'] = $desc; $arr['spbill_create_ip'] = request()->ip(); $arr['sign'] = $this->makesign($arr); //将统一下单数组 转换xml $xml = $this->toxml($arr); //post xml 到微信退款接口 $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";//微信退款地址,post请求 $ch=curl_init(); //需要获取的url地址,也可以在 curl_init() 函数中设置。 curl_setopt($ch,curlopt_url,$url); //启用时会将头文件的信息作为数据流输出。 //curl_setopt($ch,curlopt_header,1); //将 curl_exec() 获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($ch,curlopt_returntransfer,1); //证书检查 curl_setopt($ch,curlopt_ssl_verifypeer,false); //证书的类型。支持的格式有"pem" (默认值), "der"和"eng"。 curl_setopt($ch,curlopt_sslcerttype,'pem'); //一个包含pem格式证书的文件名。 curl_setopt($ch,curlopt_sslcert,gen.'/cert/apiclient_cert.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_timeout,30); //包含ssl私钥的文件名。 curl_setopt($ch,curlopt_sslkey,gen.'/cert/apiclient_key.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); //一个保存着1个或多个用来让服务端验证的证书的文件名。这个参数仅仅在和 curlopt_ssl_verifypeer 一起使用时才有意义。 . // curl_setopt($ch,curlopt_cainfo,getcwd().'/cert/rootca.pem'); curl_setopt($ch,curlopt_post,1); curl_setopt($ch,curlopt_postfields,$xml); $data=curl_exec($ch); if($data){ curl_close($ch); $data_arr = json_decode(json_encode(simplexml_load_string($data, 'simplexmlelement', libxml_nocdata)), true); return $data_arr; }else{ $error = curl_errno($ch); return "curl 错误:".$error; } } // +---------------------------------------------------------------------- // | post请求 // +---------------------------------------------------------------------- public function curlpost($url,$post_data) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); if (!empty($post_data)){ curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $post_data); } curl_setopt($ch, curlopt_returntransfer, 1); $output = curl_exec($ch); return $output; } // +---------------------------------------------------------------------- // | 齐力短信 // +---------------------------------------------------------------------- public function message($mobile){ $info = m('web') -> find(); $post_data = array(); $post_data['userid'] = $info['message_id']; $post_data['account'] = $info['message_account']; $post_data['password'] = $info['message_password']; $code = rand(1111,9999); session('code',$code); $post_data['content'] = $info['message_autograph'].'您的验证码是:'.$code.' 请务必保管好,以免泄露'; $post_data['mobile'] = $mobile; $post_data['sendtime'] = date('y-m-d'); $url='http://pt.sdqlweb.com/sms.aspx?action=send'; $o=''; foreach ($post_data as $k=>$v) { $o.="$k=".urlencode($v).'&'; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_url,$url); curl_setopt($ch, curlopt_postfields, $post_data); curl_setopt($ch, curlopt_returntransfer, 1); $data = curl_exec($ch); $data = json_decode(json_encode(simplexml_load_string($data, 'simplexmlelement', libxml_nocdata)), true); curl_close($ch); return $data; } // +---------------------------------------------------------------------- // | 以post方式提交xml到对应的接口url // +---------------------------------------------------------------------- public function postxmlcurl($xml, $url, $usecert = false, $second = 30) { $ch = curl_init(); //设置超时 curl_setopt($ch, curlopt_timeout, $second); curl_setopt($ch,curlopt_url, $url); curl_setopt($ch,curlopt_ssl_verifypeer,false); curl_setopt($ch,curlopt_ssl_verifyhost,2);//严格校验 //设置header curl_setopt($ch, curlopt_header, false); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, curlopt_returntransfer, true); //post提交方式 curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); } } // +---------------------------------------------------------------------- // | 输出xml字符 // +---------------------------------------------------------------------- public function toxml($array) { if(!is_array($array) || count($array) <= 0) { throw new wxpayexception("数组数据异常!"); } $xml = "<xml>"; foreach ($array as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![cdata[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } // +---------------------------------------------------------------------- // | 获取微信ras公钥 // +---------------------------------------------------------------------- public function get_pub_key(){ $url = "https://fraud.mch.weixin.qq.com/risk/getpublickey"; $arr['mch_id'] = $this->wechatmchid; $arr['nonce_str'] = $this->getnoncestr(); $arr['sign_type'] = 'md5'; $arr['sign'] = $this->makesign($arr); $xml = $this->toxml($arr); $ch = curl_init(); curl_setopt($ch,curlopt_url,$url); curl_setopt($ch,curlopt_returntransfer,1); curl_setopt($ch,curlopt_ssl_verifypeer,1); curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_sslcert,getcwd().'/cert/apiclient_cert.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_sslkey,getcwd().'/cert/apiclient_key.pem'); curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_cainfo,getcwd().'/cert/rootca.pem'); curl_setopt($ch,curlopt_post,1); curl_setopt($ch,curlopt_postfields,$xml); $data = $this->fromxml(curl_exec($ch)); //要创建的两个文件 $txtfilename = "./cert/public.pem"; //以读写方式打写指定文件,如果文件不存则创建 if( ($txtres=fopen ($txtfilename,"w+")) === false){ echo("创建可写文件:".$txtfilename."失败"); exit(); } echo ("创建可写文件".$txtfilename."成功!</br>"); $strconents = $data['pub_key'];//要 写进文件的内容 if(!fwrite ($txtres,$strconents)){ //将信息写入文件 echo ("尝试向文件".$txtfilename."写入".$strconents."失败!"); fclose($txtres); exit(); } echo ("尝试向文件".$txtfilename."写入".$strconents."成功!"); fclose ($txtres); //关闭指针 } // +---------------------------------------------------------------------- // | 将xml转为array // +---------------------------------------------------------------------- public function fromxml($xml) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $this->values = json_decode(json_encode(simplexml_load_string($xml, 'simplexmlelement', libxml_nocdata)), true); return $this->values; } // +---------------------------------------------------------------------- // | 微信银行卡编码 // +---------------------------------------------------------------------- public function cardcode($card_name) { $arr = array( '工商银行'=>1002, '农业银行'=>1005, '中国银行'=>1026, '建设银行'=>1003, '招商银行'=>1001, '邮储银行'=>1066, '交通银行'=>1020, '浦发银行'=>1004, '民生银行'=>1006, '兴业银行'=>1009, '平安银行'=>1010, '中信银行'=>1021, '华夏银行'=>1025, '广发银行'=>1027, '光大银行'=>1022, '北京银行'=>1032, '宁波银行'=>1056 ); foreach($arr as $k=>$v){ if($k == $card_name){ return $v; } } } // +---------------------------------------------------------------------- // | 格式化参数格式化成url参数 // +---------------------------------------------------------------------- public function tourlparams($array) { $buff = ""; foreach ($array as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } // +---------------------------------------------------------------------- // | 生成签名 本函数不覆盖sign成员变量,如要设置签名需要调用setsign方法赋值 // +---------------------------------------------------------------------- public function makesign($array) { //签名步骤一:按字典序排序参数 ksort($array); $string = $this->tourlparams($array); //签名步骤二:在string后加入key $string = $string."&key=".$this->wechatprivatekey; //签名步骤三:md5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $string = strtoupper($string); return $string; } // +---------------------------------------------------------------------- // | 产生的随机字符串 // +---------------------------------------------------------------------- public function getnoncestr($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } // +---------------------------------------------------------------------- // | 打印log日志 // +---------------------------------------------------------------------- public function save_log($msg){ error_log(date("y-m-d h:i:s")."\r\n".print_r($msg,1)."\r\n \r\n \r\n ",3,'./error.log'); } // +---------------------------------------------------------------------- // | 将图片上传至微信服务器 // +---------------------------------------------------------------------- public function curlimg($images){ $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getaccesstoken()."&type=image"; $ch1 = curl_init (); $timeout = 5; $real_path = "{$_server['document_root']}{$images}"; $data= array("media"=>"@{$real_path}",'form-data'=>$file_info); curl_setopt ( $ch1, curlopt_url, $url ); curl_setopt ( $ch1, curlopt_post, 1 ); curl_setopt ( $ch1, curlopt_returntransfer, 1 ); curl_setopt ( $ch1, curlopt_connecttimeout, $timeout ); curl_setopt ( $ch1, curlopt_ssl_verifypeer, false ); curl_setopt ( $ch1, curlopt_ssl_verifyhost, false ); curl_setopt ( $ch1, curlopt_postfields, $data ); $result = curl_exec ( $ch1 ); curl_close ( $ch1 ); if(curl_errno()==0){ $result=json_decode($result,true); return $result; }else { return false; } } // +---------------------------------------------------------------------- // | 将文章转换为微信文章 // +---------------------------------------------------------------------- public function wechattext($content){ $parrent = "/<[img|img].*?src='(.*?)'/"; $str = html_entity_decode($content); preg_match_all($parrent,$str,$match); foreach( $match[1] as $v){ $imgurl = $this->curlimg($v); $content = str_replace($v,$imgurl['url'],$content); } return ($content); } // +---------------------------------------------------------------------- // | 验证银行卡号 // +---------------------------------------------------------------------- public function check_bank_card($card){ $url = "https://ccdcapi.alipay.com/validateandcachecardinfo.json?_input_charset=utf-8&cardno={$card}&cardbincheck=true"; $data = $this->curlget($url); $name = [ "srcb"=> "深圳农村商业银行", "bgb"=> "广西北部湾银行", "shrcb"=> "上海农村商业银行", "bjbank"=> "北京银行", "whccb"=> "威海市商业银行", "bozk"=> "周口银行", "korlabank"=> "库尔勒市商业银行", "spabank"=> "平安银行", "sdeb"=> "顺德农商银行", "hurcb"=> "湖北省农村信用社", "wrcb"=> "无锡农村商业银行", "bocy"=> "朝阳银行", "czbank"=> "浙商银行", "hdbank"=> "邯郸银行", "boc"=> "中国银行", "bod"=> "东莞银行", "ccb"=> "中国建设银行", "zycbank"=> "遵义市商业银行", "sxcb"=> "绍兴银行", "gzrcu"=> "贵州省农村信用社", "zjkccb"=> "张家口市商业银行", "bojz"=> "锦州银行", "bop"=> "平顶山银行", "hkb"=> "汉口银行", "spdb"=> "上海浦东发展银行", "nxrcu"=> "宁夏黄河农村商业银行", "nynb"=> "广东南粤银行", "grcb"=> "广州农商银行", "bosz"=> "苏州银行", "hzcb"=> "杭州银行", "hsbk"=> "衡水银行", "hbc"=> "湖北银行", "jxbank"=> "嘉兴银行", "hrxjb"=> "华融湘江银行", "bodd"=> "丹东银行", "aycb"=> "安阳银行", "egbank"=> "恒丰银行", "cdb"=> "国家开发银行", "tcrcb"=> "江苏太仓农村商业银行", "njcb"=> "南京银行", "zzbank"=> "郑州银行", "dycb"=> "德阳商业银行", "ybccb"=> "宜宾市商业银行", "scrcu"=> "四川省农村信用", "klb"=> "昆仑银行", "lsbank"=> "莱商银行", "ydrcb"=> "尧都农商行", "ccqtgb"=> "重庆三峡银行", "fdb"=> "富滇银行", "jsrcu"=> "江苏省农村信用联合社", "jnbank"=> "济宁银行", "cmb"=> "招商银行", "jinchb"=> "晋城银行jcbank", "fxcb"=> "阜新银行", "whrcb"=> "武汉农村商业银行", "hbycbank"=> "湖北银行宜昌分行", "tzcb"=> "台州银行", "taccb"=> "泰安市商业银行", "xcyh"=> "许昌银行", "ceb"=> "中国光大银行", "nxbank"=> "宁夏银行", "hsbank"=> "徽商银行", "jjbank"=> "九江银行", "nhqs"=> "农信银清算中心", "mtbank"=> "浙江民泰商业银行", "langfb"=> "廊坊银行", "ascb"=> "鞍山银行", "ksrb"=> "昆山农村商业银行", "yxccb"=> "玉溪市商业银行", "dlb"=> "大连银行", "drcbcl"=> "东莞农村商业银行", "gcb"=> "广州银行", "nbbank"=> "宁波银行", "boyk"=> "营口银行", "sxrccu"=> "陕西信合", "glbank"=> "桂林银行", "boqh"=> "青海银行", "cdrcb"=> "成都农商银行", "qdccb"=> "青岛银行", "hkbea"=> "东亚银行", "hbhsbank"=> "湖北银行黄石分行", "wzcb"=> "温州银行", "trcb"=> "天津农商银行", "qlbank"=> "齐鲁银行", "gdrcc"=> "广东省农村信用社联合社", "zjtlcb"=> "浙江泰隆商业银行", "gzb"=> "赣州银行", "gycb"=> "贵阳市商业银行", "cqbank"=> "重庆银行", "daqingb"=> "龙江银行", "cgnb"=> "南充市商业银行", "sccb"=> "三门峡银行", "csrcb"=> "常熟农村商业银行", "shbank"=> "上海银行", "jlbank"=> "吉林银行", "czrcb"=> "常州农村信用联社", "bankwf"=> "潍坊银行", "zrcbank"=> "张家港农村商业银行", "fjhxbc"=> "福建海峡银行", "zjnx"=> "浙江省农村信用社联合社", "lzyh"=> "兰州银行", "jsb"=> "晋商银行", "bohaib"=> "渤海银行", "czcb"=> "浙江稠州商业银行", "yqccb"=> "阳泉银行", "sjbank"=> "盛京银行", "xabank"=> "西安银行", "bsb"=> "包商银行", "jsbank"=> "江苏银行", "fscb"=> "抚顺银行", "hnrcu"=> "河南省农村信用", "comm"=> "交通银行", "xtb"=> "邢台银行", "citic"=> "中信银行", "hxbank"=> "华夏银行", "hnrcc"=> "湖南省农村信用社", "dyccb"=> "东营市商业银行", "orbank"=> "鄂尔多斯银行", "bjrcb"=> "北京农村商业银行", "xybank"=> "信阳银行", "zgccb"=> "自贡市商业银行", "cdcb"=> "成都银行", "hanabank"=> "韩亚银行", "cmbc"=> "中国民生银行", "lybank"=> "洛阳银行", "gdb"=> "广东发展银行", "zbcb"=> "齐商银行", "cbkf"=> "开封市商业银行", "h3cb"=> "内蒙古银行", "cib"=> "兴业银行", "crcbank"=> "重庆农村商业银行", "szsbk"=> "石嘴山银行", "dzbank"=> "德州银行", "srbank"=> "上饶银行", "lsccb"=> "乐山市商业银行", "jxrcu"=> "江西省农村信用", "icbc"=> "中国工商银行", "jzbank"=> "晋中市商业银行", "hzccb"=> "湖州市商业银行", "nhb"=> "南海农村信用联社", "xxbank"=> "新乡银行", "jrcb"=> "江苏江阴农村商业银行", "ynrcc"=> "云南省农村信用社", "abc"=> "中国农业银行", "gxrcu"=> "广西省农村信用", "psbc"=> "中国邮政储蓄银行", "bzmd"=> "驻马店银行", "arcu"=> "安徽省农村信用社", "gsrcu"=> "甘肃省农村信用", "lycb"=> "辽阳市商业银行", "jlrcu"=> "吉林农信", "urmqccb"=> "乌鲁木齐市商业银行", "xlbank"=> "中山小榄村镇银行", "cscb"=> "长沙银行", "jhbank"=> "金华银行", "bhb"=> "河北银行", "nbyz"=> "鄞州银行", "lsbc"=> "临商银行", "bocd"=> "承德银行", "sdrcu"=> "山东农信", "ncb"=> "南昌银行", "tccb"=> "天津银行", "wjrcb"=> "吴江农商银行", "cbbqs"=> "城市商业银行资金清算中心", "hbrcu"=> "河北省农村信用社" ]; $bank = $data['bank']; $name = $name[$bank]; if($name){ if($bank){ $url1 = "https://apimg.alipay.com/combo.png?d=cashier&t={$bank}"; $msg['pic'] = $this->curldownload($url1,time().'.png'); $msg['logo'] = $this->imagecropper('.'.$msg['pic'],30,35); $msg['status'] = 200; $msg['name'] = $name; return $msg; } }else{ $msg['status'] = 500; $msg['data'] = '系统检测到该银行卡无效,请输入有效银行卡卡号'; return $msg; } } /** * 图像裁剪 * @param $title string 原图路径 * @param $content string 需要裁剪的宽 * @param $encode string 需要裁剪的高 */ public function imagecropper($source_path, $target_width, $target_height){ $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_x = 0; $source_y = 0; switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return false; break; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($target_width, $target_height); // 裁剪 imagecopy($cropped_image, $source_image,0,0, $source_x, $source_y, $target_width, $target_height); // 缩放 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $target_width, $target_height); imagecolortransparent($target_image,imagecolorallocate($target_image,255,255,255)); $filename = time()."1.png"; $path = './uploads/card/'.$filename; imagepng($target_image,$path); return '/uploads/card/'.$filename; } // 随机字符 public function noncestr($length = 12){ $chars = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } } ?>
下面是其他网友的补充
php获取小程序码并返回前端显示图片
小程序的二维码分为小程序码和二维码;
生成小程序二维码文档中说后端来生成。
参考 小程序开发文档资料:https://developers.weixin.qq.com/miniprogram/dev/api/getwxacodeunlimit.html
文档的参数介绍还是蛮详细的,但是没有具体的demo,对于请求的接口的返回值是进制流(也就是在浏览器显示一堆乱码)也是很令人懊恼,这里贴一下我的代码:
//获取小程序码,这里调用的是小程序码的a接口类型 public function getqrcodeaction() { $data['scene'] = $this->_req->getquery('shareid',11); //scence、page的使用要参考文档(比如:scene的值不能超过32个字符等) $data['width'] = $this->_req->getquery('width',220); $data['auto_color'] = $this->_req->getquery('auto_color'); $data['line_color'] = $this->_req->getquery('line_color'); $data['is_hyaline'] = $this->_req->getquery('is_hyaline',true); $data['page'] = $this->_req->getquery('page',""); //由这行以上代码是二维码的样式等由前端传值的形式,也可以直接在后端设置 $wxmodel = new wxauthmodel(); $token = $wxmodel->getaccesstoken(); $res_url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token"; //请求微信提供的接口 header('content-type:image/png'); $data = json_encode($data); $qr_code = $wxmodel->http_request($res_url,$data); //到这里就已经返回微信提供的返回数据了,这个时候的数据是二进制流,要处理下再返回给前端 file_put_contents('/tmp/qr_code.png', $qr_code); //将获得的数据读到一个临时图片里 $img_string = $this->filetobase64('/tmp/qr_code.png'); //将图片文件转化为base64 response::result($img_string); } //本地文件转base64 private function filetobase64($file){ $base64_file = ''; if(file_exists($file)){ $mime_type= mime_content_type($file); //如果这里明确是图片的话我建议获取图片类型这句可以省略,直接知道了mine_type='image/png',因为我这里我虽然存的图片,但是读到的mine_type值为text/plain $base64_data = base64_encode(file_get_contents($file)); $base64_file = 'data:'.$mime_type.';base64,'.$base64_data; //$base64_file = 'data:image/png;base64,'.$base64_data; } return $base64_file; } /*获取access_token,不需要code参数,不能用于获取用户信息的token*/ public function getaccesstoken() { $token_file = '/dev/shm/heka2_token.json'; //由于获取token的次数存在限制,所以将一段时间内的token缓存到一个文件(注意缓存路径服务器支持可写可读),过期后再重新获取 $data = json_decode(file_get_contents($token_file)); if ($data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret"; $res = json_decode($this->http_request($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; file_put_contents($token_file, json_encode($data)); } } else { $access_token = $data->access_token; } return $access_token; }
感觉一个完整的php实现的代码目前我还没找到,这个自己用的还行。如有不恰当的地方,欢迎指出~ _