php获取⼩程序码(B类接⼝)⽣成⼩程序码的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 = "api.weixin.qq/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 = 'api.weixin.qq/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 = 'api.weixin.qq/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);
php简单秒杀实例代码}
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 = "h.weixin.qq/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_N
OCDATA)), 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 = "h.weixin.qq/mmpaymkttransfers/promotion/transfers";//退款地址,post请求
$ch=curl_init();
//需要获取的URL地址,也可以在 curl_init() 函数中设置。
curl_setopt($ch,CURLOPT_URL,$url);
//启⽤时会将头⽂件的信息作为数据流输出。
//curl_setopt($ch,CURLOPT_HEADER,1);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论