本文實(shí)例為大家分享了php微信公眾平臺(tái)給用戶發(fā)紅包的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼:
代碼
?php /** * 微信紅包的類(lèi) * @Author snmoney#gmail.com * @copyright 2015 * @version 2.0 * *微信紅包還有部分可選的參數(shù),如分享預(yù)設(shè)值等將在后續(xù)版本補(bǔ)充上相關(guān)功能。 * 對(duì)應(yīng)官方接口更新,追加了分裂紅包的玩法,詳情參考官方文檔。 */ CLASS WXHongBao { private $mch_id = "********"; //商戶ID寫(xiě)死 private $wxappid = "********"; //微信公眾號(hào),寫(xiě)死 private $client_ip = ""; //"127.0.0.1"; //調(diào)用紅包接口的主機(jī)的IP,服務(wù)端IP,寫(xiě)死,即腳本文件所在的IP private $apikey = ""; //pay的秘鑰值 private $total_num = 1; //發(fā)放人數(shù)。固定值1,不可修改 private $nick_name = "微信公眾號(hào)紅包"; //紅包商戶名稱(chēng) private $send_name = "微信公眾號(hào)紅包"; //紅包派發(fā)者名稱(chēng) private $wishing = "歡迎再次參與"; // private $act_name = ""; //活動(dòng)名稱(chēng) private $remark = ""; private $nonce_str = ""; private $mch_billno = ""; private $re_openid = ""; //接收方的openID private $total_amount = 1 ; //紅包金額,單位 分 private $min_value = 1; //最小金額 private $max_value = 1; //根據(jù)接口要求,上述3值必須一致 private $sign = ""; //簽名在send時(shí)生成 private $amt_type; //分裂紅包參數(shù),在sendgroup中進(jìn)行定義,是常量 ALL_RAND //證書(shū),在構(gòu)造函數(shù)中定義,注意! private $apiclient_cert; //= getcwd()."/apiclient_cert.pem"; private $apiclient_key;// = getcwd()."/apiclient_key.pem"; private $apiclient_ca;// = getcwd()."/apiclient_key.pem"; //分享參數(shù) private $isShare = false; //有用?似乎是無(wú)用參數(shù),全部都不是必選和互相依賴(lài)的參數(shù) 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(){ //好像沒(méi)有什么需要構(gòu)造函數(shù)做的 引入需要的文件 $this->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() * 構(gòu)造新紅包 * @param mixed $toOpenId * @param mixed $amount 金額分 * @return void */ public function newhb($toOpenId,$amount){ if(!is_numeric($amount)){ $this->error = "金額參數(shù)錯(cuò)誤"; return; }elseif($amount100){ $this->error = "金額太小"; return; }elseif($amount>20000){ $this->error = "金額太大"; return; } $this->gen_nonce_str();//構(gòu)造隨機(jī)字串 $this->gen_mch_billno();//構(gòu)造訂單號(hào) $this->setOpenId($toOpenId); $this->setAmount($amount); $this->wxhb_inited = true; //標(biāo)記微信紅包已經(jīng)初始化完畢可以發(fā)送 //每次new 都要將分享的內(nèi)容給清空掉,否則會(huì)出現(xiàn)殘余被引用 $this->share_content= ""; $this->share_imgurl = ""; $this->share_url = ""; } /** * WXHongBao::sendGroup() * 發(fā)送裂變紅包,參數(shù)為裂變數(shù)量 * @param integer $num 3-20 * @return */ public function sendGroup($num=3){ $this->amt_type = "ALL_RAND";//$amt; 固定值。發(fā)送裂變紅包組文檔指定參數(shù),隨機(jī) 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() * 設(shè)置紅包金額 * 文檔有兩處沖突描述 * 一處指金額 >=1 (分錢(qián)) * 另一處指金額 >=100 20000 [1-200元] * 有待測(cè)試驗(yàn)證! * @param mixed $price 單位 分 * @return void */ public function setAmount($price){ $this->total_amount = (int)$price; $this->min_value = (int)$price; $this->max_value = (int)$price; } //以下方法,為設(shè)置分裂紅包時(shí)使用 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=206257621idx=1sn=56241da30e384e40771065051e4aa6a8#rd $this->share_content = $content; $this->share_imgurl = $img; $this->share_url = $url; } /** * WXHongBao::send() * 發(fā)出紅包 * 構(gòu)造簽名 * 注意第二參數(shù),單發(fā)時(shí)不要改動(dòng)! * @return boolean $success */ public function send(){ $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; $total_num = 1; if(!$this->wxhb_inited) { $this->error .= "(紅包未準(zhǔn)備好)"; return false; //未初始化完成 } $this->total_num = $total_num; $this->gen_Sign(); //生成簽名 //構(gòu)造提交的數(shù)據(jù) $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())); //確保不重復(fù)而已 } private function gen_Sign(){ unset($param); //其實(shí)應(yīng)該用key重排一次 right? $param["act_name"]=$this->act_name;// if($this->total_num==1){ //這些是裂變紅包用不上的參數(shù),會(huì)導(dǎo)致簽名錯(cuò)誤 $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的參數(shù)xml數(shù)據(jù)包 * 注意生成之前各項(xiàng)值要生成,尤其是Sign * @return $xml */ public function genXMLParam(){ // $xml = "xml> // sign>".$this->sign."/sign> // mch_billno>".$this->mch_billno."/mch_billno> // mch_id>".$this->mch_id."/mch_id> // wxappid>".$this->wxappid."/wxappid> // nick_name>![CDATA[".$this->nick_name."]]>/nick_name> // send_name>![CDATA[".$this->send_name."]]>/send_name> // re_openid>".$this->re_openid."/re_openid> // total_amount>".$this->total_amount."/total_amount> // min_value>".$this->min_value."/min_value> // max_value>".$this->max_value."/max_value> // total_num>".$this->total_num."/total_num> // wishing>![CDATA[".$this->wishing."]]>/wishing> // client_ip>![CDATA[".$this->client_ip."]]>/client_ip> // act_name>![CDATA[".$this->act_name."]]>/act_name> // remark>![CDATA[".$this->remark."]]>/remark> // nonce_str>".$this->nonce_str."/nonce_str>"; $xml = "xml> act_name>![CDATA[".$this->act_name."]]>/act_name> client_ip>![CDATA[".$this->client_ip."]]>/client_ip> max_value>".$this->max_value."/max_value> mch_billno>".$this->mch_billno."/mch_billno> mch_id>".$this->mch_id."/mch_id> min_value>".$this->min_value."/min_value> nick_name>![CDATA[".$this->nick_name."]]>/nick_name> nonce_str>".$this->nonce_str."/nonce_str> re_openid>".$this->re_openid."/re_openid> remark>![CDATA[".$this->remark."]]>/remark> send_name>![CDATA[".$this->send_name."]]>/send_name> total_amount>".$this->total_amount."/total_amount> total_num>".$this->total_num."/total_num> wishing>![CDATA[".$this->wishing."]]>/wishing> wxappid>".$this->wxappid."/wxappid> sign>".$this->sign."/sign> /xml> "; // nick_name>![CDATA[".$this->nick_name."]]>/nick_name> // min_value>".$this->min_value."/min_value> // max_value>".$this->max_value."/max_value> // $xml .="/xml>"; return $xml; } /** * WXHongBao::gen_mch_billno() * 商戶訂單號(hào)(每個(gè)訂單號(hào)必須唯一) 組成: mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字。 接口根據(jù)商戶訂單號(hào)支持重入, 如出現(xiàn)超時(shí)可再調(diào)用。 * @return void */ private function gen_mch_billno(){ //生成一個(gè)長(zhǎng)度10,的阿拉伯?dāng)?shù)字隨機(jī)字符串 $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.上邊是紅包類(lèi),需要用的時(shí)候直接引入紅包類(lèi)。 *2.//實(shí)例化紅包類(lèi) * $wxhongbao=new \WXHongBao(); *3. //需要發(fā)放的openid 金額 openid 根據(jù)微信提供的接口獲取,金額根據(jù)自己需求 * $wxhongbao->newhb($user_openid, $pay_money*100); *$wxhongbao->setActName("根據(jù)自己需求設(shè)置"); * $wxhongbao->setWishing("根據(jù)自己需求設(shè)置"); *$wxhongbao->setRemark("根據(jù)自己需求設(shè)置"); *參數(shù)設(shè)置之后發(fā)放紅包 *$wxhongbao->send(); **/ ?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:山東 林芝 張家口 成都 威海 梅州 巴中 泰州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實(shí)現(xiàn)微信公眾平臺(tái)發(fā)紅包功能》,本文關(guān)鍵詞 php,實(shí)現(xiàn),微信,公眾,平臺(tái),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。