: This article mainly introduces the encapsulation of red packet distribution. if you are interested in the PHP Tutorial, refer to it.
Issuance configuration
Issuing tools
_ Keys [$ key] = $ config [$ key];} catch (Exception $ e) {throw new Exception ('Parameter missing :'. $ key) ;}} foreach ($ files as $ file) {try {$ cret_file = $ config [$ file]; if (is_file ($ cret_file )) {$ this-> _ cert [$ file] = $ cret_file;} catch (Exception $ e) {throw new Exception ('certificate error ');}}} /*** red packet issuing ** @ param array $ parameters required parameter * @ return boolean */public function sendredpack ($ parameters) {// officially required $ stime = date ('H: I '); If ($ stime <'08: 00') {$ this-> error = 'Please trigger a red envelope gift away from-Beijing time '; return false ;} $ bonus = array (); $ this-> setParameter ('mch _ id', $ this-> _ keys ['mch _ id']); $ this-> setParameter ('wxappid ', $ this-> _ keys ['wxappid']); $ must = array ('re _ openid', 'Nick _ name ', 'Send _ name', 'total _ amount', 'wishing', 'Act _ name', 'remark', 'Client _ IP '); foreach ($ must as $ key) {if (isset ($ parameters [$ key]) & $ parameters [$ key]) {$ this-> se TParameter ($ key, $ parameters [$ key]);} else if (! Isset ($ this-> _ parameters [$ key]) |! $ This-> _ parameters [$ key]) {$ this-> error = 'parameter defect: '. $ key; return false ;}} if (! Isset ($ parameters ['mch _ billno']) {$ parameters ['mch _ billno'] = $ this-> getMchBillno ();} $ this-> setParameter ('mch _ billno', $ parameters ['mch _ billno']); $ this-> setParameter ('total _ num', 1 ); $ this-> setParameter ('nonce _ str', $ this-> getRand (30, 3); $ postXml = $ this-> _ createXml (); if (! $ PostXml) {return false;} $ this-> log ($ postXml, 'Send _ XML'); $ result = $ this-> curl_post_ssl (self: SENDREDPACK_URL, $ postXml); $ this-> log ($ result, 'result _ XML'); if (! $ Result) {return false;} $ resultObj = simplexml_load_string ($ result, 'simplexmlelement', LIBXML_NOCDATA); $ this-> _ lastResult = $ resultObj; if ($ resultObj-> return_code = 'success') {// if ($ resultObj-> result_code = 'success') {return $ resultObj-> send_listid ;} if ($ resultObj-> return_msg) {$ this-> error = (string) $ resultObj-> return_msg; return false;} $ this-> error = (string) $ resultObj-> err_code_des; Return false;} if ($ resultObj-> return_code! = 'Failed') {$ this-> error = 'return information format exception'; return false;} $ this-> error = (string) $ resultObj-> return_msg; return false;}/*** query the details of a red envelope * parameter: * mch_billno, the unique order number generated when the red envelope is sent * return: * success: object * failure: false **/public function getInfo ($ mch_billno) {$ param = array ('nonce _ str' => $ this-> getRand (30, 3 ), 'mch _ billno' => $ mch_billno, 'mch _ id' => $ this-> _ keys ['mch _ id'], 'appid '=> $ this-> _ keys ['wxappid'], 'Bill _ type' => 'mch T',); ksort ($ param); $ unSignParaString = $ this-> _ formatQueryParaMap ($ param, false ); $ param ['sign'] = $ this-> _ sign ($ unSignParaString, $ this-> _ keys ['key']); $ xml = $ this-> arrayToXml ($ param); $ this-> log ($ xml, 'getinfo _ XML'); $ result = $ this-> curl_post_ssl (WePay:: GETHBINFO_URL, $ xml); if (! $ Result) {return false;} $ this-> log ($ result, 'result _ XML'); $ resultObj = simplexml_load_string ($ RESULT, 'simplexmlelement', LIBXML_NOCDATA ); $ this-> _ lastResult = $ resultObj; if ($ resultObj-> return_code = 'success ') {// if ($ resultObj-> result_code = 'success') {return $ resultObj;} if ($ resultObj-> return_msg) {$ this-> error = $ resultObj-> return_msg; return false;} $ this-> error = $ resultObj-> err_code_des; Return false;} if ($ resultObj-> return_code! = 'Failed') {$ this-> error = 'return information format exception'; return false;} $ this-> error = $ resultObj-> return_msg; return false ;} /*** set the parameter * @ param $ parameter key value array/Key * @ param $ value * @ return WxBonusApi */public function setParameter ($ parameter, $ value = null) {if (! Is_array ($ parameter) {return $ this-> setParameter (array ($ parameter => $ value);} foreach ($ parameter as $ key => $ value) {$ key = trim ($ key); $ value = trim ($ value); $ this-> _ parameters [$ key] = $ value;} return $ this ;} /*** get the parameter value * @ param $ parameter key name * @ return multitype: */public function getParameter ($ parameter) {return $ this-> _ parameters [$ parameter];}/*** obtain the random number * @ param number $ the number of digits of the len random number * @ param numb Er $ type value range: 1: Number 2 lowercase letters 4 uppercase letters * @ return string */public function getRand ($ len = 30, $ type = 0) {$ str = ''; $ max =-1; if (! $ Type) {$ type = 3;} if ($ type & 1) {$ str. = '000000'; $ max + = 10;} if ($ type & 2) {$ str. = 'abcdefghijklmnopqrstuvwxy'; $ max + = 26;} if ($ type & 4) {$ str. = 'abcdefghijklmnopqrstuvwxy'; $ max + = 26 ;}$ rand = ''; for ($ I = 0; $ I <$ len; $ I ++) {$ rand. = $ str [rand (0, $ max)];} return $ rand;}/*** generate the merchant's order number * @ return string */public function getMchBillno () {$ this-> _ law.chbillno = $ this-> _ parameters ['mch _ id ']. Date ('ymdhis '). $ this-> getRand (4, 1); // $ this-> getRandNum (); return $ this-> _ law.chbillno ;} /*** get the order number generated when the last red envelope was created * @ return string */public function getlaw.chbillno () {return $ this-> _ law.chbillno ;} /*** method for creating XML * @ param number $ retcode * @ param string $ reterrmsg * @ return boolean | string */private function _ createXml () {try {$ sign = $ this-> _ getSign (); if (! $ Sign) {return false;} $ this-> setParameter ('sign', $ sign); return $ this-> arrayToXml ($ this-> _ parameters );} catch (Exception $ e) {$ this-> error = $ e-> getMessage (); return false ;}} /*** convert the parameter to XML * @ param array $ arr parameter array * @ return string */public function arrayToXml ($ arr) {$ xml ="
"; Foreach ($ arr as $ key => $ val) {if (is_numeric ($ val) {$ xml. = "<". $ key. "> ". $ val."
";} Else {$ xml. =" <". $ key.">" . $val . "
";}}$ Xml. ="
"; Return $ xml;}/*** obtain the signature result * @ return boolean | Ambigous
*/Protected function _ getSign () {try {if ($ this-> _ checkSign () = false) {// check whether the generated signature parameter $ this-> error = 'is missing! '; $ This-> log (json_encode ($ this-> _ parameters, JSON_UNESCAPED_UNICODE), 'error _ Sign_XML'); return false ;} ksort ($ this-> _ parameters); $ unSignParaString = $ this-> _ formatQueryParaMap ($ this-> _ parameters, false ); return $ this-> _ sign ($ unSignParaString, $ this-> _ keys ['key']);} catch (Exception $ e) {$ this-> error = $ e-> getMessage (); return false ;}} /*** check whether all parameters required for the signature are complete * @ return boolean */private function _ checkSign () {// Return true; if ($ this-> _ parameters ["nonce_str"] = null | $ this-> _ parameters ["mch_billno"] = null | $ this-> _ parameters ["mch_id"] = null | $ this-> _ parameters ["wxappid"] = null | $ this-> _ parameters ["nick_name"] = null | $ this-> _ parameters ["send_name"] = null | $ this-> _ parameters ["re_openid"] = null | $ this-> _ parameters ["total_amount"] = null | $ this-> _ parameters ["total_num"] = nu Ll | $ this-> _ parameters ["wishing"] = null | $ this-> _ parameters ["client_ip"] = null | $ this-> _ parameters ["act_name"] = null | $ this-> _ parameters ["remark"] = null) {return false;} return true;}/***** @ param $ paraMap * @ param $ urlencode * @ return string */private function _ formatQueryParaMap ($ paraMap, $ urlencode) {$ buff = ""; ksort ($ paraMap); foreach ($ paraMap as $ k => $ v) {if (null! = $ V & "null "! = $ V & "sign "! = $ K) {if ($ urlencode) {$ v = urlencode ($ v);} $ buff. = $ k. "= ". $ v. "&" ;}}$ reqPar; if (strlen ($ buff)> 0) {$ reqPar = substr ($ buff, 0, strlen ($ buff)-1 );} return $ reqPar ;} /*** signature * @ param $ content: The signature string * @ param $ key * @ throws Exception * @ return string | boolean */private function _ sign ($ content, $ key) {try {if (null = $ key) {$ this-> error = 'signature key cannot be blank! '; Return false;} if (null = $ content) {$ this-> error = 'signature content cannot be blank'; return false;} $ signStr = $ content. "& key = ". $ key; return strtoupper (md5 ($ signStr);} catch (Exception $ e) {$ this-> error = $ e-> getMessage (); return false ;}} /*** cURL capture ** @ param $ url * @ param $ vars parameter * @ param * $ second * @ param * $ aHeader * @ return mixed | boolean */ function curl_post_ssl ($ url, $ data, $ second = 30, $ aHeader = array () {$ ch = curl_init (); // timeout value: curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // Set the proxy here. if so, curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, success, false ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); // The cert and key belong to two. pem file curl_setopt ($ ch, CURLOPT_SSLCERT, $ this-> _ cert ['api _ cert']); curl_setopt ($ ch, CURLOPT_SSLKEY, $ this-> _ cert ['api _ key']); curl_setopt ($ ch, CURLOPT_CAINFO, $ this-> _ cert ['rootca']); if (count ($ aHeader)> = 1) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ aHeader);} curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); $ data = curl_exec ($ ch); if ($ data) {curl_close ($ ch); return $ data ;} else {$ this-> log (json_encode ($ this-> _ cert); $ this-> error = 'AA :'. curl_errno ($ ch); curl_close ($ ch); return false;}/*** get server ip ** @ return string */public function getServerIp () {$ server_ip = '2017. 0.0.1 '; if (isset ($ _ SERVER) {if (isset ($ _ SERVER ['server _ ADDR']) & $ _ SERVER ['server _ ADDR ']) {$ server_ip = $ _ SERVER ['server _ ADDR'];} elseif (isset ($ _ SERVER ['Local _ ADDR ']) & $ _ SERVER ['Local _ ADDR']) {$ server_ip = $ _ SERVER ['Local _ ADDR '] ;}} else {$ server_ip = getenv ('server _ ADDR');} return $ server_ip ;} /*** set the log directory file ** @ param unknown $ file */public function setLogFile ($ file) {$ this-> log_file = $ file ;} /*** write log ** @ param $ msg the information written * @ param $ type the log type as the query identifier */public function log ($ msg, $ type) {if ($ this-> log_file) {$ log = str_replace (array ("\ r \ n", "\ r", "\ n"), array ("", "", ""), $ msg); error_log ($ type. ''. date ('Y-m-d H: I: s '). ''. $ log. "\ r \ n", 3, $ this-> log_file );}}}
Test issuance demo
WxBonusConfig: APPID, 'mch _ id' => WxBonusConfig: MCHID, 'key' => WxBonusConfig: key, 'api _ cert' => WxBonusConfig :: getRealPath (). wxBonusConfig: SSLCERT_PATH, // all three paths are absolute paths 'api _ key' => WxBonusConfig: getRealPath (). wxBonusConfig: SSLKEY_PATH, 'rootca '=> WxBonusConfig: getRealPath (). wxBonusConfig: SSLROOTCA); $ param = array ('re _ openid' => 'openid', // The sent openid 'Nick _ name' => 'test ', // useless, but the required parameter 'send _ name' => 'test', // The name of the received red packet 'total _ amount' => '123 ', // The amount is divided into 'wishing' => 'Shanghai', // red envelope greetings, no more than 32 characters 'act _ name' => 'activity name', // activity name, 'remark' => 'note', // remarks are not displayed, and 'client _ IP' => $ WxBonus-> getServerIp () // server ip address sent) is not displayed ); $ WxBonus-> setLogFile ('d: \ log1.txt '); // Set the log file $ result = $ WxBonus-> sendredpack ($ param ); // send a red packet // The result is true/false. if it is false, the error message is in error if ($ result) {echo "OK"; return true ;} echo $ WxBonus-> error; return false ;}}
The code is attached:
Link: http://pan.baidu.com/s/1o7p6GXG password: shjz
The above describes the red packet distribution package, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.