侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php实现微信公众平台发红包功能

微信(小程序)  /  管理员 发布于 7年前   169

本文实例为大家分享了php微信公众平台给用户发红包的具体代码,供大家参考,具体内容如下

直接上代码:

代码

wxhb_inited = false;     $this->apiclient_cert = getcwd() . "/zzz1/apiclient_cert.pem";    $this->apiclient_key = getcwd() . "/zzz1/apiclient_key.pem";    $this->apiclient_ca = getcwd() . "/zzz1/rootca.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::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;  }  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 = (int)$price;    $this->min_value = (int)$price;    $this->max_value = (int)$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;  }  /**   * 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();    //echo $xml;    //debug    file_put_contents("hbxml.debug",$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);    curl_setopt($ch,CURLOPT_CAINFO,$this->appclient_ca);    /*     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);     //die(print_r($data));    if($data){      curl_close($ch);      $rsxml = simplexml_load_string($data);      if($rsxml->return_code == 'SUCCESS' ){        return true;      }else{        $this->error = json_encode($rsxml->return_msg);        return false;        }    }else{       $this->error = curl_errno($ch);      curl_close($ch);      return false;    }  }  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("11sign.txt",$sign_raw);//debug    $this->sign = strtoupper(md5($sign_raw));  }  /**   * WXHongBao::genXMLParam()   * 生成post的参数xml数据包   * 注意生成之前各项值要生成,尤其是Sign   * @return $xml   */  public function genXMLParam(){    // $xml = "    //   ".$this->sign."     //   ".$this->mch_billno."     //   ".$this->mch_id."     //   ".$this->wxappid."     //   nick_name."]]>     //   send_name."]]>     //   ".$this->re_openid."     //   ".$this->total_amount."     //   ".$this->min_value."     //   ".$this->max_value."     //   ".$this->total_num."     //   wishing."]]>     //   client_ip."]]>     //   act_name."]]>     //   remark."]]>           //   ".$this->nonce_str."";   $xml = "         act_name."]]>          client_ip."]]>          ".$this->max_value."           ".$this->mch_billno."           ".$this->mch_id."          ".$this->min_value."           nick_name."]]>           ".$this->nonce_str."          ".$this->re_openid."           remark."]]>           send_name."]]>           ".$this->total_amount."".$this->total_num."           wishing."]]>           ".$this->wxappid."           ".$this->sign."                    ";      // nick_name."]]>       // ".$this->min_value."       // ".$this->max_value."     // $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;  }}/***1.上边是红包类,需要用的时候直接引入红包类。*2.//实例化红包类*  $wxhongbao=new \WXHongBao();*3. //需要发放的openid 金额 openid 根据微信提供的接口获取,金额根据自己需求* $wxhongbao->newhb($user_openid, $pay_money*100);*$wxhongbao->setActName("根据自己需求设置");* $wxhongbao->setWishing("根据自己需求设置");*$wxhongbao->setRemark("根据自己需求设置");*参数设置之后发放红包*$wxhongbao->send();**/?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • PHP版微信公众平台红包API
  • 使用PHP实现微信摇一摇周边红包
  • php实现微信发红包
  • php版微信公众号接口实现发红包的方法
  • php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
  • PHP实现微信发红包程序
  • PHP微信红包API接口
  • php实现的微信红包算法分析(非官方)
  • php版微信发红包接口用法示例
  • PHP微信红包生成代码分享


  • 上一条:
    PHP实现 APP端微信支付功能
    下一条:
    php微信开发之关注事件
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客