欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php微信公众帐号发送红包

程序员文章站 2022-04-25 23:16:36
...
开发框架为we7

所需参数:appid,appSecret,MchId,API密钥

php
/**
 * 微信红包的类 
 * 
 */CLASS WXHongBao {
    
    private$mch_id = "111111";//商户ID写死private$wxappid = "22222222";//微信公众号,写死private$client_ip = "119.29.80.114"; //调用红包接口的主机的IP,服务端IP,写死,即脚本文件所在的IPprivate$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";
    }
    
    publicfunction err(){
        return$this->error;
    } 
    publicfunction error(){
        return$this->err();
    }
    /**
     * WXHongBao::newhb()
     * 构造新红包 
     * @param mixed $toOpenId
     * @param mixed $amount 金额分
     * @return void
     */publicfunction newhb($toOpenId,$amount){
         
        if(!is_numeric($amount)){
            $this->error = "金额参数错误";
            return;
        }elseif($amount){