php实现微信发红包实现微信发红包
本文实例讲述了php实现的微信红包算法。分享给大家供大家参考。具体如下:
具体代码:
<?php
/**
* 微信红包的类
*
*/
CLASS WXHongBao {
private $mch_id = "111111";//商户ID写死
private $wxappid = "22222222";//微信公众号,写死
private $client_ip = "119.29.80.114"; //调用红包接口的主机的IP,服务端IP,写死,即脚本文件所在的IP
private $apikey = "33333333";//pay的秘钥值
private $total_num = 1;//发放人数。固定值1,不可修改
private $nick_name = "微信产品中心公众号"; //红包商户名称
private $send_name = "微信产品中心公众号";//红包派发者名称
private $wishing = "祝福语"; //
private $act_name = "红包活动"; //活动名称
private $remark = "活动备注";
private $nonce_str = "";
private $mch_billno = "";
private $re_openid = "";//接收方的openID
private $total_amount = 1 ;//红包金额,单位 分
private $min_value = 1;//最小金额
private $max_value = 1; //根据接口要求,上述3值必须一致
private $sign = ""; //签名在send时生成
private $amt_type; //分裂红包参数,在sendgroup中进行定义,是常量 ALL_RAND
//证书,在构造函数中定义,注意!
private $apiclient_cert; //= getcwd()."/apiclient_cert.pem";
private $apiclient_key;// = getcwd()."/apiclient_key.pem";
//分享参数
private $isShare = false; //有用?似乎是无用参数,全部都不是必选和互相依赖的参数
private $share_content = "";
private $share_url ="";
private $share_imgurl = "";
private $wxhb_inited;
private $api_hb_group = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";//裂变红包
private $api_hb_single = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
private $error = "ok"; //init
/**
* WXHongBao::__construct()
* 步骤
* new(openid,amount)
* setnickname
* setsend_name
* setwishing
* setact_name
* setremark
* send()
* @return void
*/
function __construct(){
//好像没有什么需要构造函数做的
$this->wxhb_inited = false;
$this->apiclient_cert = getcwd()."/apiclient_cert.pem";
$this->apiclient_key = getcwd()."/apiclient_key.pem";
}
public function err(){
return $this->error;
}
public function error(){
return $this->err();
}
/**
* WXHongBao::newhb()
* 构造新红包
* @param mixed $toOpenId
* @param mixed $amount 金额分