代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?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 金额分
@return void
/
public function newhb($toOpenId,$amount){
if(!is_numeric($amount)){
$this->error = \公众金额参数缺点\"大众;
return;
}elseif($amount<100){
$this->error = \公众金额太小\"大众;
return;
}elseif($amount>20000){
$this->error = \公众金额太大\公众;
return;
}
$this->gen_nonce_str();//布局随机字串
$this->gen_mch_billno();//布局订单号
$this->setOpenId($toOpenId);
$this->setAmount($amount);
$this->wxhb_inited = true; //标记微信红包已经初始化完毕可以发送
//每次new 都要将分享的内容给清空掉,否则会涌现残余被引用
$this->share_content= \"大众\"大众;
$this->share_imgurl = \公众\"大众;
$this->share_url = \公众\"大众;
}
/
WXHongBao::send()
发出红包
布局署名
把稳第二参数,单发时不要改动!
@return boolean $success
/
public function send($url = \"大众https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack\"大众,$total_num = 1){
if(!$this->wxhb_inited){
$this->error .= \"大众(红包未准备好)\"大众;
return false; //未初始化完成
}
$this->total_num = $total_num;
$this->gen_Sign(); //天生署名
//布局提交的数据
$xml = $this->genXMLParam();
//debug
file_put_contents(\"大众hbxml.txt\"大众,$xml);
//提交xml,curl
//$url = \"大众https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack\"大众;
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,10);
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,$this->apiclient_cert);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,$this->apiclient_key);
/
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
/
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
$rsxml = simplexml_load_string($data);
if($rsxml->return_code == 'SUCCESS' ){
return true;
}else{
$this->error = $rsxml->return_msg;
return false;
}
}else{
$this->error = curl_errno($ch);
curl_close($ch);
return false;
}
}
/
WXHongBao::sendGroup()
发送裂变红包,参数为裂变数量
@param integer $num 3-20
@return
/
public function sendGroup($num=3){
$this->amt_type = \"大众ALL_RAND\"大众;//$amt; 固定值。发送裂变红包组文档指定参数,随机
return $this->send($this->api_hb_group,$num);
}
public function getApiSingle(){
return $this->api_hb_single;
}
public function getApiGroup(){
return $this->api_hb_group;
}
public function setNickName($nick){
$this->nick_name = $nick;
}
public function setSendName($name){
$this->send_name = $name;
}
public function setWishing($wishing){
$this->wishing = $wishing;
}
/
WXHongBao::setActName()
活动名称
@param mixed $act
@return void
/
public function setActName($act){
$this->act_name = $act;
}
public function setRemark($remark){
$this->remark = $remark;
}
public function setOpenId($openid){
$this->re_openid = $openid;
}
/
WXHongBao::setAmount()
设置红包金额
文档有两处冲突描述
一处指金额 >=1 (分钱)
另一处指金额 >=100 < 20000 [1-200元]
有待测试验证!
@param mixed $price 单位 分
@return void
/
public function setAmount($price){
$this->total_amount = $price;
$this->min_value = $price;
$this->max_value = $price;
}
//以下方法,为设置分裂红包时利用
public function setHBminmax($min,$max){
$this->min_value = $min;
$this->max_value = $max;
}
public function setShare($img=\公众\"大众,$url=\"大众\"大众,$content=\"大众\公众){
//https://mmbiz.qlogo.cn/mmbiz/MS1jaDO92Ep4qNo9eV0rnItptyBrzUhJqT8oxSsCofdxibnNWMJiabaqgLPkDaEJmia6fqTXAXulKBa9NLfxYMwYA/0?wx_fmt=png
//http://mp.weixin.qq.com/s?__biz=MzA5Njg4NTk3MA==&mid=206257621&idx=1&sn=56241da30e384e40771065051e4aa6a8#rd
$this->share_content = $content;
$this->share_imgurl = $img;
$this->share_url = $url;
}
private function gen_nonce_str(){
$this->nonce_str = strtoupper(md5(mt_rand().time())); //确保不重复而已
}
private function gen_Sign(){
unset($param);
//实在该当用key重排一次 right?
$param[\公众act_name\"大众]=$this->act_name;//
if($this->total_num==1){ //这些是裂变红包用不上的参数,会导致署名缺点
$param[\"大众client_ip\"大众]=$this->client_ip;
$param[\"大众max_value\公众]=$this->max_value;
$param[\"大众min_value\公众]=$this->min_value;
$param[\"大众nick_name\"大众]=$this->nick_name;
}
$param[\公众mch_billno\公众] = $this->mch_billno; //
$param[\"大众mch_id\"大众]=$this->mch_id;//
$param[\公众nonce_str\"大众]=$this->nonce_str; //
$param[\"大众re_openid\"大众]=$this->re_openid;//
$param[\"大众remark\"大众]=$this->remark; //
$param[\公众send_name\公众]=$this->send_name;//
$param[\"大众total_amount\公众]=$this->total_amount;//
$param[\公众total_num\公众]=$this->total_num; //
$param[\公众wishing\"大众]=$this->wishing;//
$param[\公众wxappid\"大众]=$this->wxappid;//
if($this->share_content) $param[\"大众share_content\公众] = $this->share_content;
if($this->share_imgurl) $param[\公众share_imgurl\"大众] = $this->share_imgurl;
if($this->share_url) $param[\"大众share_url\"大众] = $this->share_url;
if($this->amt_type) $param[\"大众amt_type\公众] = $this->amt_type; //
ksort($param); //按照键名排序...艹,上面排了我好久
//$sign_raw = http_build_query($param).\公众&key=\"大众.$this->apikey;
$sign_raw = \公众\"大众;
foreach($param as $k => $v){
$sign_raw .= $k.\公众=\公众.$v.\"大众&\"大众;
}
$sign_raw .= \"大众key=\公众.$this->apikey;
//file_put_contents(\"大众sign.raw\公众,$sign_raw);//debug
$this->sign = strtoupper(md5($sign_raw));
}
/
WXHongBao::genXMLParam()
天生post的参数xml数据包
把稳天生之前各项值要天生,尤其是Sign
@return $xml
/
public function genXMLParam(){
$xml = \公众<xml>
<sign>\"大众.$this->sign.\"大众</sign>
<mch_billno>\公众.$this->mch_billno.\"大众</mch_billno>
<mch_id>\"大众.$this->mch_id.\"大众</mch_id>
<wxappid>\公众.$this->wxappid.\"大众</wxappid>
<nick_name><![CDATA[\"大众.$this->nick_name.\公众]]></nick_name>
<send_name><![CDATA[\"大众.$this->send_name.\公众]]></send_name>
<re_openid>\公众.$this->re_openid.\公众</re_openid>
<total_amount>\"大众.$this->total_amount.\公众</total_amount>
<min_value>\"大众.$this->min_value.\公众</min_value>
<max_value>\"大众.$this->max_value.\"大众</max_value>
<total_num>\"大众.$this->total_num.\"大众</total_num>
<wishing><![CDATA[\"大众.$this->wishing.\公众]]></wishing>
<client_ip><![CDATA[\"大众.$this->client_ip.\"大众]]></client_ip>
<act_name><![CDATA[\"大众.$this->act_name.\公众]]></act_name>
<remark><![CDATA[\"大众.$this->remark.\公众]]></remark>
<nonce_str>\"大众.$this->nonce_str.\"大众</nonce_str>
\公众;
if($this->share_content) $xml .= \公众<share_content><![CDATA[\公众.$this->share_content.\"大众]]></share_content>
\"大众;
if($this->share_imgurl) $xml .= \"大众<share_imgurl><![CDATA[\"大众.$this->share_imgurl.\"大众]]></share_imgurl>
\公众;
if($this->share_url) $xml .= \公众<share_url><![CDATA[\公众.$this->share_url.\"大众]]></share_url>
\"大众;
if($this->amt_type) $xml .= \"大众<amt_type><![CDATA[\"大众.$this->amt_type.\"大众]]></amt_type>
\"大众;
$xml .=\公众</xml>\公众;
return $xml;
}
/
WXHongBao::gen_mch_billno()
商户订单号(每个订单号必须唯一)
组成: mch_id+yyyymmdd+10位一天内不能重复的数字。
接口根据商户订单号支持重入, 如涌现超时可再调用。
@return void
/
private function gen_mch_billno(){
//天生一个长度10,的阿拉伯数字随机字符串
$rnd_num = array('0','1','2','3','4','5','6','7','8','9');
$rndstr = \"大众\"大众;
while(strlen($rndstr)<10){
$rndstr .= $rnd_num[array_rand($rnd_num)];
}
$this->mch_billno = $this->mch_id.date(\"大众Ymd\"大众).$rndstr;
}
}
?>
然后实例化class.
$toOpenId = 'asdasdasd'; //吸收红包的用户的微信OpenId
$hb = new WXHongBao();
$hb->newhb($toOpenId,1000); //新建一个10元的红包,第二参数单位是 分,把稳取值范围 1-200元
//以下多少项可选操作,不指定则利用class脚本顶部的预设值
$hb->setNickName(\公众土豪有限公司\"大众);
$hb->setSendName(\公众土豪\"大众);
$hb->setWishing(\公众恭喜发财\"大众);
$hb->setActName(\"大众发钱活动\公众);
$hb->setRemark(\"大众任性一把\"大众);
//发送红包
if(!$hb->send()){ //发送缺点
echo $hb->err();
}else{
echo \公众红包发送成功\"大众;
}
总结:以上便是本篇文的全部内容,希望能对大家的学习有所帮助。
干系推举:
php中浮点数运算实例剖析
PHP定时任务获取微信access_token的方法实例分享
php基于递归实现删除文件目录的方法
以上便是php微信发红包算法的详细内容,更多请关注其它干系文章!
更多技巧请《转发 + 关注》哦!