php微信公众帐号发送红包

WBOY
Libérer: 2016-07-29 09:11:01
original
1027 Les gens l'ont consulté

开发框架为we7

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

<span>php
</span><span>/*</span><span>*
 * 微信红包的类 
 * 
 </span><span>*/</span><span>CLASS</span><span> WXHongBao {
    
    </span><span>private</span><span>$mch_id</span> = "111111";<span>//</span><span>商户ID写死</span><span>private</span><span>$wxappid</span> = "22222222";<span>//</span><span>微信公众号,写死</span><span>private</span><span>$client_ip</span> = "119.29.80.114"; <span>//</span><span>调用红包接口的主机的IP,服务端IP,写死,即脚本文件所在的IP</span><span>private</span><span>$apikey</span> = "33333333";<span>//</span><span>pay的秘钥值</span><span>private</span><span>$total_num</span> = 1;<span>//</span><span>发放人数。固定值1,不可修改    </span><span>private</span><span>$nick_name</span> = "微信产品中心公众号"; <span>//</span><span>红包商户名称</span><span>private</span><span>$send_name</span> = "微信产品中心公众号";<span>//</span><span>红包派发者名称</span><span>private</span><span>$wishing</span> = "祝福语"; <span>//</span><span>private</span><span>$act_name</span> = "红包活动"; <span>//</span><span>活动名称</span><span>private</span><span>$remark</span> = "活动备注"<span>;
    </span><span>private</span><span>$nonce_str</span> = ""<span>;
    </span><span>private</span><span>$mch_billno</span> = ""<span>;
    </span><span>private</span><span>$re_openid</span> = "";<span>//</span><span>接收方的openID    </span><span>private</span><span>$total_amount</span> = 1 ;<span>//</span><span>红包金额,单位 分</span><span>private</span><span>$min_value</span> = 1;<span>//</span><span>最小金额</span><span>private</span><span>$max_value</span> = 1; <span>//</span><span>根据接口要求,上述3值必须一致             </span><span>private</span><span>$sign</span> = ""; <span>//</span><span>签名在send时生成    </span><span>private</span><span>$amt_type</span>; <span>//</span><span>分裂红包参数,在sendgroup中进行定义,是常量 ALL_RAND 
    
    //证书,在构造函数中定义,注意!</span><span>private</span><span>$apiclient_cert</span>; <span>//</span><span>= getcwd()."/apiclient_cert.pem";</span><span>private</span><span>$apiclient_key</span>;<span>//</span><span> = getcwd()."/apiclient_key.pem";
    
    //分享参数</span><span>private</span><span>$isShare</span> = <span>false</span>; <span>//</span><span>有用?似乎是无用参数,全部都不是必选和互相依赖的参数</span><span>private</span><span>$share_content</span> = ""<span>; 
    </span><span>private</span><span>$share_url</span> =""<span>;
    </span><span>private</span><span>$share_imgurl</span> = ""<span>;
    
    </span><span>private</span><span>$wxhb_inited</span><span>;
    
    </span><span>private</span><span>$api_hb_group</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";<span>//</span><span>裂变红包</span><span>private</span><span>$api_hb_single</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"<span>;
    
    </span><span>private</span><span>$error</span> = "ok"; <span>//</span><span>init</span><span>/*</span><span>*
     * WXHongBao::__construct()
     * 步骤
     * new(openid,amount)
     * setnickname
     * setsend_name
     * setwishing
     * setact_name
     * setremark
     * send()
     * @return void
     </span><span>*/</span><span>function</span><span> __construct(){
        </span><span>//</span><span>好像没有什么需要构造函数做的</span><span>$this</span>->wxhb_inited = <span>false</span><span>; 
        </span><span>$this</span>->apiclient_cert = <span>getcwd</span>()."/apiclient_cert.pem"<span>;
        </span><span>$this</span>->apiclient_key = <span>getcwd</span>()."/apiclient_key.pem"<span>;
    }
    
    </span><span>public</span><span>function</span><span> err(){
        </span><span>return</span><span>$this</span>-><span>error;
    } 
    </span><span>public</span><span>function</span><span> error(){
        </span><span>return</span><span>$this</span>-><span>err();
    }
    </span><span>/*</span><span>*
     * WXHongBao::newhb()
     * 构造新红包 
     * @param mixed $toOpenId
     * @param mixed $amount 金额分
     * @return void
     </span><span>*/</span><span>public</span><span>function</span> newhb(<span>$toOpenId</span>,<span>$amount</span><span>){
         
        </span><span>if</span>(!<span>is_numeric</span>(<span>$amount</span><span>)){
            </span><span>$this</span>->error = "金额参数错误"<span>;
            </span><span>return</span><span>;
        }</span><span>elseif</span>(<span>$amount</span>){
            <span>$this</span>->error = "金额太小"<span>;
            </span><span>return</span><span>;
        }</span><span>elseif</span>(<span>$amount</span>>20000<span>){
            </span><span>$this</span>->error = "金额太大"<span>;
            </span><span>return</span><span>;
        }
       
        </span><span>$this</span>->gen_nonce_str();<span>//</span><span>构造随机字串</span><span>$this</span>->gen_mch_billno();<span>//</span><span>构造订单号</span><span>$this</span>->setOpenId(<span>$toOpenId</span><span>);
        </span><span>$this</span>->setAmount(<span>$amount</span><span>);
        </span><span>$this</span>->wxhb_inited = <span>true</span>; <span>//</span><span>标记微信红包已经初始化完毕可以发送
        
        //每次new 都要将分享的内容给清空掉,否则会出现残余被引用</span><span>$this</span>->share_c>;
        <span>$this</span>->share_imgurl = ""<span>;
        </span><span>$this</span>->share_url = ""<span>;
    }
    
    </span><span>/*</span><span>*
     * WXHongBao::send()
     * 发出红包
     * 构造签名
     * 注意第二参数,单发时不要改动!
     * @return boolean $success
     </span><span>*/</span><span>public</span><span>function</span> send(<span>$url</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack",<span>$total_num</span> = 1<span>){
    
        </span><span>if</span>(!<span>$this</span>-><span>wxhb_inited){
            </span><span>$this</span>->error .= "(红包未准备好)"<span>;
            </span><span>return</span><span>false</span>; <span>//</span><span>未初始化完成</span><span>        }
        
        </span><span>$this</span>->total_num = <span>$total_num</span><span>;
        
        </span><span>$this</span>->gen_Sign(); <span>//</span><span>生成签名
        
        //构造提交的数据        </span><span>$xml</span> = <span>$this</span>-><span>genXMLParam();
        
        
        </span><span>//</span><span>debug</span><span>file_put_contents</span>("hbxml.txt",<span>$xml</span><span>);
        
        </span><span>//</span><span>提交xml,curl
        //$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";</span><span>$ch</span> =<span> curl_init();        
        curl_setopt(</span><span>$ch</span>,CURLOPT_TIMEOUT,10<span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_RETURNTRANSFER, 1<span>);        
        curl_setopt(</span><span>$ch</span>,CURLOPT_URL,<span>$url</span><span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSL_VERIFYPEER,<span>false</span><span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSL_VERIFYHOST,<span>false</span><span>);
        
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSLCERT,<span>$this</span>-><span>apiclient_cert);        
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSLKEYTYPE,'PEM'<span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_SSLKEY,<span>$this</span>-><span>apiclient_key);
        
        </span><span>/*</span><span>
        if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        </span><span>*/</span><span>
        curl_setopt(</span><span>$ch</span>,CURLOPT_POST, 1<span>);
        curl_setopt(</span><span>$ch</span>,CURLOPT_POSTFIELDS,<span>$xml</span><span>);
        </span><span>$data</span> = curl_exec(<span>$ch</span><span>);
        </span><span>if</span>(<span>$data</span><span>){
            curl_close(</span><span>$ch</span><span>);    
            </span><span>$rsxml</span> = <span>simplexml_load_string</span>(<span>$data</span><span>);
            </span><span>if</span>(<span>$rsxml</span>->return_code == 'SUCCESS'<span> ){
                </span><span>return</span><span>true</span><span>;
            }</span><span>else</span><span>{
                </span><span>$this</span>->error = <span>$rsxml</span>-><span>return_msg;
                </span><span>return</span><span>false</span><span>;    
            }
            
        }</span><span>else</span><span>{ 
            </span><span>$this</span>->error = curl_errno(<span>$ch</span><span>);
             
            curl_close(</span><span>$ch</span><span>);
            </span><span>return</span><span>false</span><span>;
        }

    }
    
    </span><span>/*</span><span>*
     * WXHongBao::sendGroup()
     * 发送裂变红包,参数为裂变数量
     * @param integer $num 3-20
     * @return
     </span><span>*/</span><span>public</span><span>function</span> sendGroup(<span>$num</span>=3<span>){
        </span><span>$this</span>->amt_type = "ALL_RAND";<span>//</span><span>$amt; 固定值。发送裂变红包组文档指定参数,随机</span><span>return</span><span>$this</span>->send(<span>$this</span>->api_hb_group,<span>$num</span><span>);
    }
    
    </span><span>public</span><span>function</span><span> getApiSingle(){
        </span><span>return</span><span>$this</span>-><span>api_hb_single;
    }
    
    </span><span>public</span><span>function</span><span> getApiGroup(){
        </span><span>return</span><span>$this</span>-><span>api_hb_group;
    }
    
    </span><span>public</span><span>function</span> setNickName(<span>$nick</span><span>){
        </span><span>$this</span>->nick_name = <span>$nick</span><span>;
    }
    
    </span><span>public</span><span>function</span> setSendName(<span>$name</span><span>){
        </span><span>$this</span>->send_name = <span>$name</span><span>;
    }
    
    </span><span>public</span><span>function</span> setWishing(<span>$wishing</span><span>){
        </span><span>$this</span>->wishing = <span>$wishing</span><span>;
    }
    
    </span><span>/*</span><span>*
     * WXHongBao::setActName()
     * 活动名称 
     * @param mixed $act
     * @return void
     </span><span>*/</span><span>public</span><span>function</span> setActName(<span>$act</span><span>){
        </span><span>$this</span>->act_name = <span>$act</span><span>;
    }
    
    </span><span>public</span><span>function</span> setRemark(<span>$remark</span><span>){
        </span><span>$this</span>->remark = <span>$remark</span><span>;
    }
    
    </span><span>public</span><span>function</span> setOpenId(<span>$openid</span><span>){
        </span><span>$this</span>->re_openid = <span>$openid</span><span>;
    }
    
    </span><span>/*</span><span>*
     * WXHongBao::setAmount()
     * 设置红包金额
     * 文档有两处冲突描述 
     * 一处指金额 >=1 (分钱)
     * 另一处指金额 >=100 <span>*/</span><span>public</span><span>function</span> setAmount(<span>$price</span><span>){
        </span><span>$this</span>->total_amount = <span>$price</span><span>;
        </span><span>$this</span>->min_value = <span>$price</span><span>;
        </span><span>$this</span>->max_value = <span>$price</span><span>;
    }
    </span><span>//</span><span>以下方法,为设置分裂红包时使用</span><span>public</span><span>function</span> setHBminmax(<span>$min</span>,<span>$max</span><span>){
        </span><span>$this</span>->min_value = <span>$min</span><span>;
        </span><span>$this</span>->max_value = <span>$max</span><span>;
    }
    
    
    </span><span>public</span><span>function</span> setShare(<span>$img</span>="",<span>$url</span>="",<span>$content</span>=""<span>){
        
        </span><span>//</span><span>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</span><span>$this</span>->share_content = <span>$content</span><span>;
        </span><span>$this</span>->share_imgurl = <span>$img</span><span>;
        </span><span>$this</span>->share_url = <span>$url</span><span>;
    }
    
    </span><span>private</span><span>function</span><span> gen_nonce_str(){
        </span><span>$this</span>->nonce_str = <span>strtoupper</span>(<span>md5</span>(<span>mt_rand</span>().<span>time</span>())); <span>//</span><span>确保不重复而已</span><span>    }
    
    </span><span>private</span><span>function</span><span> gen_Sign(){
        </span><span>unset</span>(<span>$param</span><span>); 
        </span><span>//</span><span>其实应该用key重排一次 right?</span><span>$param</span>["act_name"]=<span>$this</span>->act_name;<span>//
</span><span>if</span>(<span>$this</span>->total_num==1){ <span>//</span><span>这些是裂变红包用不上的参数,会导致签名错误</span><span>$param</span>["client_ip"]=<span>$this</span>-><span>client_ip;
            </span><span>$param</span>["max_value"]=<span>$this</span>-><span>max_value;
            </span><span>$param</span>["min_value"]=<span>$this</span>-><span>min_value;
            </span><span>$param</span>["nick_name"]=<span>$this</span>-><span>nick_name;
        }
        
        </span><span>$param</span>["mch_billno"] = <span>$this</span>->mch_billno;   <span>//</span><span>$param</span>["mch_id"]=<span>$this</span>->mch_id;<span>//</span><span>$param</span>["nonce_str"]=<span>$this</span>->nonce_str;    <span>//</span><span>$param</span>["re_openid"]=<span>$this</span>->re_openid;<span>//
</span><span>$param</span>["remark"]=<span>$this</span>->remark;        <span>//
</span><span>$param</span>["send_name"]=<span>$this</span>->send_name;<span>//
</span><span>$param</span>["total_amount"]=<span>$this</span>->total_amount;<span>//
</span><span>$param</span>["total_num"]=<span>$this</span>->total_num;        <span>//
</span><span>$param</span>["wishing"]=<span>$this</span>->wishing;<span>//
</span><span>$param</span>["wxappid"]=<span>$this</span>->wxappid;<span>//
</span><span>if</span>(<span>$this</span>->share_content) <span>$param</span>["share_content"] = <span>$this</span>-><span>share_content;
        </span><span>if</span>(<span>$this</span>->share_imgurl) <span>$param</span>["share_imgurl"] = <span>$this</span>-><span>share_imgurl;
        </span><span>if</span>(<span>$this</span>->share_url) <span>$param</span>["share_url"] = <span>$this</span>-><span>share_url;
        
        </span><span>if</span>(<span>$this</span>->amt_type) <span>$param</span>["amt_type"] = <span>$this</span>->amt_type; <span>//
</span><span>ksort</span>(<span>$param</span>); <span>//</span><span>按照键名排序...艹,上面排了我好久
        
        //$sign_raw = http_build_query($param)."&key=".$this->apikey;</span><span>$sign_raw</span> = ""<span>;
        </span><span>foreach</span>(<span>$param</span><span>as</span><span>$k</span> => <span>$v</span><span>){
            </span><span>$sign_raw</span> .= <span>$k</span>."=".<span>$v</span>."&"<span>;
        }
        </span><span>$sign_raw</span> .= "key=".<span>$this</span>-><span>apikey;
        
        </span><span>//</span><span>file_put_contents("sign.raw",$sign_raw);//debug</span><span>$this</span>->sign = <span>strtoupper</span>(<span>md5</span>(<span>$sign_raw</span><span>));
    }
    
    </span><span>/*</span><span>*
     * WXHongBao::genXMLParam()
     * 生成post的参数xml数据包
     * 注意生成之前各项值要生成,尤其是Sign
     * @return $xml
     </span><span>*/</span><span>public</span><span>function</span><span> genXMLParam(){
        
        </span><span>$xml</span> = "<span><xml>
            <sign></sign></xml></span>".<span>$this</span>->sign."<span> 
            <mch_billno></mch_billno></span>".<span>$this</span>->mch_billno."<span> 
            <mch_id></mch_id></span>".<span>$this</span>->mch_id."<span> 
            <wxappid></wxappid></span>".<span>$this</span>->wxappid."<span> 
            <nick_name>".<span>$this</span>->nick_name."<span>]]></span></nick_name> 
            <send_name>".<span>$this</span>->send_name."<span>]]></span></send_name> 
            <re_openid></re_openid></span>".<span>$this</span>->re_openid."<span> 
            <total_amount></total_amount></span>".<span>$this</span>->total_amount."<span> 
            <min_value></min_value></span>".<span>$this</span>->min_value."<span> 
            <max_value></max_value></span>".<span>$this</span>->max_value."<span> 
            <total_num></total_num></span>".<span>$this</span>->total_num."<span> 
            <wishing>".<span>$this</span>->wishing."<span>]]></span></wishing> 
            <client_ip>".<span>$this</span>->client_ip."<span>]]></span></client_ip> 
            <act_name>".<span>$this</span>->act_name."<span>]]></span></act_name> 
            <remark>".<span>$this</span>->remark."<span>]]></span></remark>             
            <nonce_str></nonce_str></span>".<span>$this</span>->nonce_str."<span>
            </span>"<span>; 
        
            
        </span><span>if</span>(<span>$this</span>->share_content) <span>$xml</span> .= "<share_content>$this</share_content></span>->share_content."<span>]]>
        </span>"<span>;
        </span><span>if</span>(<span>$this</span>->share_imgurl) <span>$xml</span> .= "<share_imgurl>$this->share_imgurl."<span>]]></span></share_imgurl>
        "<span>;
        </span><span>if</span>(<span>$this</span>->share_url) <span>$xml</span> .= "<share_url>$this->share_url."<span>]]></span></share_url>
        "<span>;
        </span><span>if</span>(<span>$this</span>->amt_type) <span>$xml</span> .= "<amt_type>$this->amt_type."<span>]]></span></amt_type>
        "<span>;
        
        </span><span>$xml</span> .=""<span>;
        
        </span><span>return</span><span>$xml</span><span>;
    }
    
    </span><span>/*</span><span>*
     * WXHongBao::gen_mch_billno()
     *  商户订单号(每个订单号必须唯一) 
        组成: mch_id+yyyymmdd+10位一天内不能重复的数字。 
        接口根据商户订单号支持重入, 如出现超时可再调用。 
     * @return void
     </span><span>*/</span><span>private</span><span>function</span><span> gen_mch_billno(){
        </span><span>//</span><span>生成一个长度10,的阿拉伯数字随机字符串</span><span>$rnd_num</span> = <span>array</span>('0','1','2','3','4','5','6','7','8','9'<span>);
        </span><span>$rndstr</span> = ""<span>;
        </span><span>while</span>(<span>strlen</span>(<span>$rndstr</span>)){
            <span>$rndstr</span> .= <span>$rnd_num</span>[<span>array_rand</span>(<span>$rnd_num</span><span>)];    
        }
        
        </span><span>$this</span>->mch_billno = <span>$this</span>->mch_id.<span>date</span>("Ymd").<span>$rndstr</span><span>;
    }
}    
</span>?><br>然后实例化class.<br>
Copier après la connexion

<span>    $toOpenId</span> = 'asdasdasd'; <span>//</span><span>接收红包的用户的微信OpenId</span><span>$hb</span> = <span>new</span><span> WXHongBao();<br></span><span>$hb</span>->newhb(<span>$toOpenId</span>,1000); <span>//</span><span>新建一个10元的红包,第二参数单位是 分,注意取值范围 1-200元
        //以下若干项可选操作,不指定则使用class脚本顶部的预设值</span><span>$hb</span>->setNickName("土豪有限公司"<span>);
          </span><span>$hb</span>->setSendName("土豪"<span>);
          </span><span>$hb</span>->setWishing("恭喜发财"<span>);
          </span><span>$hb</span>->setActName("发钱活动"<span>);
          </span><span>$hb</span>->setRemark("任性一把"<span>);

        </span><span>//</span><span>发送红包</span><span>if</span>(!<span>$hb</span>->send()){ <span>//</span><span>发送错误</span><span>echo</span><span>$hb</span>-><span>err();

        }</span><span>else</span><span>{

           </span><span>echo</span> "红包发送成功"<span>;

        }</span>
Copier après la connexion

以上就介绍了php微信公众帐号发送红包,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!