PHP WeChat パブリック アカウントが赤い封筒を送信します
開発フレームワークは we7 です
必須パラメータ: appid、appSecret、MchId、API キー
<?<span style="color: #000000;">php</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 微信红包的类 * </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">CLASS</span><span style="color: #000000;"> WXHongBao { </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$mch_id</span> = "111111";<span style="color: #008000;">//</span><span style="color: #008000;">商户ID写死</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$wxappid</span> = "22222222";<span style="color: #008000;">//</span><span style="color: #008000;">微信公众号,写死</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$client_ip</span> = "119.29.80.114"; <span style="color: #008000;">//</span><span style="color: #008000;">调用红包接口的主机的IP,服务端IP,写死,即脚本文件所在的IP</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$apikey</span> = "33333333";<span style="color: #008000;">//</span><span style="color: #008000;">pay的秘钥值</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$total_num</span> = 1;<span style="color: #008000;">//</span><span style="color: #008000;">发放人数。固定值1,不可修改 </span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$nick_name</span> = "微信产品中心公众号"; <span style="color: #008000;">//</span><span style="color: #008000;">红包商户名称</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$send_name</span> = "微信产品中心公众号";<span style="color: #008000;">//</span><span style="color: #008000;">红包派发者名称</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$wishing</span> = "祝福语"; <span style="color: #008000;">//</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$act_name</span> = "红包活动"; <span style="color: #008000;">//</span><span style="color: #008000;">活动名称</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$remark</span> = "活动备注"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$nonce_str</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$mch_billno</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$re_openid</span> = "";<span style="color: #008000;">//</span><span style="color: #008000;">接收方的openID </span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$total_amount</span> = 1 ;<span style="color: #008000;">//</span><span style="color: #008000;">红包金额,单位 分</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$min_value</span> = 1;<span style="color: #008000;">//</span><span style="color: #008000;">最小金额</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$max_value</span> = 1; <span style="color: #008000;">//</span><span style="color: #008000;">根据接口要求,上述3值必须一致 </span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$sign</span> = ""; <span style="color: #008000;">//</span><span style="color: #008000;">签名在send时生成 </span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$amt_type</span>; <span style="color: #008000;">//</span><span style="color: #008000;">分裂红包参数,在sendgroup中进行定义,是常量 ALL_RAND //证书,在构造函数中定义,注意!</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$apiclient_cert</span>; <span style="color: #008000;">//</span><span style="color: #008000;">= getcwd()."/apiclient_cert.pem";</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$apiclient_key</span>;<span style="color: #008000;">//</span><span style="color: #008000;"> = getcwd()."/apiclient_key.pem"; //分享参数</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$isShare</span> = <span style="color: #0000ff;">false</span>; <span style="color: #008000;">//</span><span style="color: #008000;">有用?似乎是无用参数,全部都不是必选和互相依赖的参数</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$share_content</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$share_url</span> =""<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$share_imgurl</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$wxhb_inited</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$api_hb_group</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";<span style="color: #008000;">//</span><span style="color: #008000;">裂变红包</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$api_hb_single</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$error</span> = "ok"; <span style="color: #008000;">//</span><span style="color: #008000;">init</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::__construct() * 步骤 * new(openid,amount) * setnickname * setsend_name * setwishing * setact_name * setremark * send() * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){ </span><span style="color: #008000;">//</span><span style="color: #008000;">好像没有什么需要构造函数做的</span> <span style="color: #800080;">$this</span>->wxhb_inited = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->apiclient_cert = <span style="color: #008080;">getcwd</span>()."/apiclient_cert.pem"<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->apiclient_key = <span style="color: #008080;">getcwd</span>()."/apiclient_key.pem"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> err(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">error; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> error(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">err(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::newhb() * 构造新红包 * @param mixed $toOpenId * @param mixed $amount 金额分 * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> newhb(<span style="color: #800080;">$toOpenId</span>,<span style="color: #800080;">$amount</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_numeric</span>(<span style="color: #800080;">$amount</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$this</span>->error = "金额参数错误"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$amount</span><100<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->error = "金额太小"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$amount</span>>20000<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->error = "金额太大"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } </span><span style="color: #800080;">$this</span>->gen_nonce_str();<span style="color: #008000;">//</span><span style="color: #008000;">构造随机字串</span> <span style="color: #800080;">$this</span>->gen_mch_billno();<span style="color: #008000;">//</span><span style="color: #008000;">构造订单号</span> <span style="color: #800080;">$this</span>->setOpenId(<span style="color: #800080;">$toOpenId</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->setAmount(<span style="color: #800080;">$amount</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->wxhb_inited = <span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;">标记微信红包已经初始化完毕可以发送 //每次new 都要将分享的内容给清空掉,否则会出现残余被引用</span> <span style="color: #800080;">$this</span>->share_content= ""<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->share_imgurl = ""<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->share_url = ""<span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::send() * 发出红包 * 构造签名 * 注意第二参数,单发时不要改动! * @return boolean $success </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> send(<span style="color: #800080;">$url</span> = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack",<span style="color: #800080;">$total_num</span> = 1<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>-><span style="color: #000000;">wxhb_inited){ </span><span style="color: #800080;">$this</span>->error .= "(红包未准备好)"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>; <span style="color: #008000;">//</span><span style="color: #008000;">未初始化完成</span><span style="color: #000000;"> } </span><span style="color: #800080;">$this</span>->total_num = <span style="color: #800080;">$total_num</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->gen_Sign(); <span style="color: #008000;">//</span><span style="color: #008000;">生成签名 //构造提交的数据 </span> <span style="color: #800080;">$xml</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">genXMLParam(); </span><span style="color: #008000;">//</span><span style="color: #008000;">debug</span> <span style="color: #008080;">file_put_contents</span>("hbxml.txt",<span style="color: #800080;">$xml</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">提交xml,curl //$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";</span> <span style="color: #800080;">$ch</span> =<span style="color: #000000;"> curl_init(); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_TIMEOUT,10<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_URL,<span style="color: #800080;">$url</span><span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSL_VERIFYPEER,<span style="color: #0000ff;">false</span><span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSL_VERIFYHOST,<span style="color: #0000ff;">false</span><span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERT,<span style="color: #800080;">$this</span>-><span style="color: #000000;">apiclient_cert); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLKEYTYPE,'PEM'<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLKEY,<span style="color: #800080;">$this</span>-><span style="color: #000000;">apiclient_key); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> if( count($aHeader) >= 1 ){ curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); } </span><span style="color: #008000;">*/</span><span style="color: #000000;"> curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_POST, 1<span style="color: #000000;">); curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080;">$xml</span><span style="color: #000000;">); </span><span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">){ curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">); </span><span style="color: #800080;">$rsxml</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$rsxml</span>->return_code == 'SUCCESS'<span style="color: #000000;"> ){ </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$this</span>->error = <span style="color: #800080;">$rsxml</span>-><span style="color: #000000;">return_msg; </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$this</span>->error = curl_errno(<span style="color: #800080;">$ch</span><span style="color: #000000;">); curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::sendGroup() * 发送裂变红包,参数为裂变数量 * @param integer $num 3-20 * @return </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> sendGroup(<span style="color: #800080;">$num</span>=3<span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->amt_type = "ALL_RAND";<span style="color: #008000;">//</span><span style="color: #008000;">$amt; 固定值。发送裂变红包组文档指定参数,随机</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->send(<span style="color: #800080;">$this</span>->api_hb_group,<span style="color: #800080;">$num</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getApiSingle(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">api_hb_single; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getApiGroup(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">api_hb_group; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setNickName(<span style="color: #800080;">$nick</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->nick_name = <span style="color: #800080;">$nick</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setSendName(<span style="color: #800080;">$name</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->send_name = <span style="color: #800080;">$name</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setWishing(<span style="color: #800080;">$wishing</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->wishing = <span style="color: #800080;">$wishing</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::setActName() * 活动名称 * @param mixed $act * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setActName(<span style="color: #800080;">$act</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->act_name = <span style="color: #800080;">$act</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setRemark(<span style="color: #800080;">$remark</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->remark = <span style="color: #800080;">$remark</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setOpenId(<span style="color: #800080;">$openid</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->re_openid = <span style="color: #800080;">$openid</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::setAmount() * 设置红包金额 * 文档有两处冲突描述 * 一处指金额 >=1 (分钱) * 另一处指金额 >=100 < 20000 [1-200元] * 有待测试验证! * @param mixed $price 单位 分 * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setAmount(<span style="color: #800080;">$price</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->total_amount = <span style="color: #800080;">$price</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->min_value = <span style="color: #800080;">$price</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->max_value = <span style="color: #800080;">$price</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">以下方法,为设置分裂红包时使用</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setHBminmax(<span style="color: #800080;">$min</span>,<span style="color: #800080;">$max</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->min_value = <span style="color: #800080;">$min</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->max_value = <span style="color: #800080;">$max</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setShare(<span style="color: #800080;">$img</span>="",<span style="color: #800080;">$url</span>="",<span style="color: #800080;">$content</span>=""<span style="color: #000000;">){ </span><span style="color: #008000;">//</span><span style="color: #008000;">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 style="color: #800080;">$this</span>->share_content = <span style="color: #800080;">$content</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->share_imgurl = <span style="color: #800080;">$img</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->share_url = <span style="color: #800080;">$url</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> gen_nonce_str(){ </span><span style="color: #800080;">$this</span>->nonce_str = <span style="color: #008080;">strtoupper</span>(<span style="color: #008080;">md5</span>(<span style="color: #008080;">mt_rand</span>().<span style="color: #008080;">time</span>())); <span style="color: #008000;">//</span><span style="color: #008000;">确保不重复而已</span><span style="color: #000000;"> } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> gen_Sign(){ </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$param</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">其实应该用key重排一次 right?</span> <span style="color: #800080;">$param</span>["act_name"]=<span style="color: #800080;">$this</span>->act_name;<span style="color: #008000;">//</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->total_num==1){ <span style="color: #008000;">//</span><span style="color: #008000;">这些是裂变红包用不上的参数,会导致签名错误</span> <span style="color: #800080;">$param</span>["client_ip"]=<span style="color: #800080;">$this</span>-><span style="color: #000000;">client_ip; </span><span style="color: #800080;">$param</span>["max_value"]=<span style="color: #800080;">$this</span>-><span style="color: #000000;">max_value; </span><span style="color: #800080;">$param</span>["min_value"]=<span style="color: #800080;">$this</span>-><span style="color: #000000;">min_value; </span><span style="color: #800080;">$param</span>["nick_name"]=<span style="color: #800080;">$this</span>-><span style="color: #000000;">nick_name; } </span><span style="color: #800080;">$param</span>["mch_billno"] = <span style="color: #800080;">$this</span>->mch_billno; <span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["mch_id"]=<span style="color: #800080;">$this</span>->mch_id;<span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["nonce_str"]=<span style="color: #800080;">$this</span>->nonce_str; <span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["re_openid"]=<span style="color: #800080;">$this</span>->re_openid;<span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["remark"]=<span style="color: #800080;">$this</span>->remark; <span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["send_name"]=<span style="color: #800080;">$this</span>->send_name;<span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["total_amount"]=<span style="color: #800080;">$this</span>->total_amount;<span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["total_num"]=<span style="color: #800080;">$this</span>->total_num; <span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["wishing"]=<span style="color: #800080;">$this</span>->wishing;<span style="color: #008000;">//</span> <span style="color: #800080;">$param</span>["wxappid"]=<span style="color: #800080;">$this</span>->wxappid;<span style="color: #008000;">//</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_content) <span style="color: #800080;">$param</span>["share_content"] = <span style="color: #800080;">$this</span>-><span style="color: #000000;">share_content; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_imgurl) <span style="color: #800080;">$param</span>["share_imgurl"] = <span style="color: #800080;">$this</span>-><span style="color: #000000;">share_imgurl; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_url) <span style="color: #800080;">$param</span>["share_url"] = <span style="color: #800080;">$this</span>-><span style="color: #000000;">share_url; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->amt_type) <span style="color: #800080;">$param</span>["amt_type"] = <span style="color: #800080;">$this</span>->amt_type; <span style="color: #008000;">//</span> <span style="color: #008080;">ksort</span>(<span style="color: #800080;">$param</span>); <span style="color: #008000;">//</span><span style="color: #008000;">按照键名排序...艹,上面排了我好久 //$sign_raw = http_build_query($param)."&key=".$this->apikey;</span> <span style="color: #800080;">$sign_raw</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$param</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">){ </span><span style="color: #800080;">$sign_raw</span> .= <span style="color: #800080;">$k</span>."=".<span style="color: #800080;">$v</span>."&"<span style="color: #000000;">; } </span><span style="color: #800080;">$sign_raw</span> .= "key=".<span style="color: #800080;">$this</span>-><span style="color: #000000;">apikey; </span><span style="color: #008000;">//</span><span style="color: #008000;">file_put_contents("sign.raw",$sign_raw);//debug</span> <span style="color: #800080;">$this</span>->sign = <span style="color: #008080;">strtoupper</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$sign_raw</span><span style="color: #000000;">)); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::genXMLParam() * 生成post的参数xml数据包 * 注意生成之前各项值要生成,尤其是Sign * @return $xml </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> genXMLParam(){ </span><span style="color: #800080;">$xml</span> = "<span style="color: #000000;"><xml> <sign></span>".<span style="color: #800080;">$this</span>->sign."<span style="color: #000000;"></sign> <mch_billno></span>".<span style="color: #800080;">$this</span>->mch_billno."<span style="color: #000000;"></mch_billno> <mch_id></span>".<span style="color: #800080;">$this</span>->mch_id."<span style="color: #000000;"></mch_id> <wxappid></span>".<span style="color: #800080;">$this</span>->wxappid."<span style="color: #000000;"></wxappid> <nick_name><![CDATA[</span>".<span style="color: #800080;">$this</span>->nick_name."<span style="color: #000000;">]]></nick_name> <send_name><![CDATA[</span>".<span style="color: #800080;">$this</span>->send_name."<span style="color: #000000;">]]></send_name> <re_openid></span>".<span style="color: #800080;">$this</span>->re_openid."<span style="color: #000000;"></re_openid> <total_amount></span>".<span style="color: #800080;">$this</span>->total_amount."<span style="color: #000000;"></total_amount> <min_value></span>".<span style="color: #800080;">$this</span>->min_value."<span style="color: #000000;"></min_value> <max_value></span>".<span style="color: #800080;">$this</span>->max_value."<span style="color: #000000;"></max_value> <total_num></span>".<span style="color: #800080;">$this</span>->total_num."<span style="color: #000000;"></total_num> <wishing><![CDATA[</span>".<span style="color: #800080;">$this</span>->wishing."<span style="color: #000000;">]]></wishing> <client_ip><![CDATA[</span>".<span style="color: #800080;">$this</span>->client_ip."<span style="color: #000000;">]]></client_ip> <act_name><![CDATA[</span>".<span style="color: #800080;">$this</span>->act_name."<span style="color: #000000;">]]></act_name> <remark><![CDATA[</span>".<span style="color: #800080;">$this</span>->remark."<span style="color: #000000;">]]></remark> <nonce_str></span>".<span style="color: #800080;">$this</span>->nonce_str."<span style="color: #000000;"></nonce_str> </span>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_content) <span style="color: #800080;">$xml</span> .= "<share_content><![CDATA[".<span style="color: #800080;">$this</span>->share_content."<span style="color: #000000;">]]></share_content> </span>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_imgurl) <span style="color: #800080;">$xml</span> .= "<share_imgurl><![CDATA[".<span style="color: #800080;">$this</span>->share_imgurl."<span style="color: #000000;">]]></share_imgurl> </span>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->share_url) <span style="color: #800080;">$xml</span> .= "<share_url><![CDATA[".<span style="color: #800080;">$this</span>->share_url."<span style="color: #000000;">]]></share_url> </span>"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->amt_type) <span style="color: #800080;">$xml</span> .= "<amt_type><![CDATA[".<span style="color: #800080;">$this</span>->amt_type."<span style="color: #000000;">]]></amt_type> </span>"<span style="color: #000000;">; </span><span style="color: #800080;">$xml</span> .="</xml>"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$xml</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * WXHongBao::gen_mch_billno() * 商户订单号(每个订单号必须唯一) 组成: mch_id+yyyymmdd+10位一天内不能重复的数字。 接口根据商户订单号支持重入, 如出现超时可再调用。 * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> gen_mch_billno(){ </span><span style="color: #008000;">//</span><span style="color: #008000;">生成一个长度10,的阿拉伯数字随机字符串</span> <span style="color: #800080;">$rnd_num</span> = <span style="color: #0000ff;">array</span>('0','1','2','3','4','5','6','7','8','9'<span style="color: #000000;">); </span><span style="color: #800080;">$rndstr</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$rndstr</span>)<10<span style="color: #000000;">){ </span><span style="color: #800080;">$rndstr</span> .= <span style="color: #800080;">$rnd_num</span>[<span style="color: #008080;">array_rand</span>(<span style="color: #800080;">$rnd_num</span><span style="color: #000000;">)]; } </span><span style="color: #800080;">$this</span>->mch_billno = <span style="color: #800080;">$this</span>->mch_id.<span style="color: #008080;">date</span>("Ymd").<span style="color: #800080;">$rndstr</span><span style="color: #000000;">; }} </span>?><br><br>然后实例化class.<br><br></span></span></span></span></span></span></span>
<span style="color: #800080;"> $toOpenId</span> = 'asdasdasd'; <span style="color: #008000;">//</span><span style="color: #008000;">接收红包的用户的微信OpenId</span> <span style="color: #800080;">$hb</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> WXHongBao();<br></span> <span style="color: #800080;">$hb</span>->newhb(<span style="color: #800080;">$toOpenId</span>,1000); <span style="color: #008000;">//</span><span style="color: #008000;">新建一个10元的红包,第二参数单位是 分,注意取值范围 1-200元 //以下若干项可选操作,不指定则使用class脚本顶部的预设值</span> <span style="color: #800080;">$hb</span>->setNickName("土豪有限公司"<span style="color: #000000;">); </span><span style="color: #800080;">$hb</span>->setSendName("土豪"<span style="color: #000000;">); </span><span style="color: #800080;">$hb</span>->setWishing("恭喜发财"<span style="color: #000000;">); </span><span style="color: #800080;">$hb</span>->setActName("发钱活动"<span style="color: #000000;">); </span><span style="color: #800080;">$hb</span>->setRemark("任性一把"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">发送红包</span> <span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$hb</span>->send()){ <span style="color: #008000;">//</span><span style="color: #008000;">发送错误</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$hb</span>-><span style="color: #000000;">err(); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "红包发送成功"<span style="color: #000000;">; }</span>