PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php实现微信发红包

    作者:admin来源:网络浏览:时间:2020-09-30 00:11:53我要评论
    导读:本文实例讲述了php实现的微信红包算法。分享给大家供大家参考。具体如下:

    本文实例讲述了php实现的微信红包算法。分享给大家供大家参考。具体如下:

    php实现微信发红包

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

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-6296-1.html
    相关热词搜索: php 微信发红包