生成小程序码的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实现的代码目前我还没找到,这个自己用的还行。如有不恰当的地方,欢迎指出~ _