This article mainly introduces the development of PHP cash red envelope function, combined with the case form more detailed analysis of PHP using the interface to achieve cash red envelope function of the relevant steps and operation skills, the need for friends can refer to the next
Specific as follows:
Business background-cash red packet development
Sdk
<?phpclass Wxpay {//config parameter information const SHANGHUHAO = "1430998xxx";//merchant number Const Partnerkey = "Lefytoxwzuexxfp47wou5d9xxxxxx" ; API, Merchant backend//core payment function, Parameters: request address and parameter function pay ($url, $obj) {$obj [' nonce_str '] = $this->create_noncestr (); Creates a random string $stringA = $this->create_qianming ($obj, false); Create Signature $stringSignTemp = $stringA. " &key=lefytoxwzuexxfp47wou5xxxxxx "; Signature Add API $sign = Strtoupper (MD5 ($stringSignTemp)); The signature is encrypted and capitalized $obj [' sign '] = $sign; Incoming signature to array $postXml = $this->arraytoxml ($obj); Convert parameters to XML format var_dump ($POSTXML); $RESPONSEXML = $this->curl_post_ssl ($url, $postXml); Submit Request Var_dump ($RESPONSEXML); return $responseXml; }//Generate signature, parameters: Generate signature parameters and whether to encode function create_qianming ($arr, $urlencode) {$buff = ""; Ksort ($arr); The contents of the incoming array parameter are sorted alphabetically, a in front, z in the Last (dictionary order) foreach ($arr as $k + $v) {if (null!= $v && "null"! = $v &&am P "Sign"! = $k) {//signature do not transcode if ($urlencode) {$v = UrlEncode ($v); } $buff. = $k. " = ". $v." & "; }} if (Strlen ($buff) >0) {$reqPar = substr ($buff, 0,strlen ($buff)-1);//Remove the end symbol "&"} return $reqPar ; }//Generate random string, default 32-bit function create_noncestr ($length =32) {//create random character $chars = "Abcdefghijklmnopqrstuvwxyzabcdefghijkl mnopqrstuvwxyz0123456789 "; $str = ""; for ($i =0; $i < $length; $i + +) {$str. =substr ($chars, Mt_rand (0,strlen ($chars)-1), 1); } return $STR; }//Array to XML function Arraytoxml ($arr) {$xml = "<xml>"; foreach ($arr as $key = + $val) {if (Is_numeric ($val)) {$xml. = "<". $key. " > ". $val." </". $key." > "; } else {$xml. = "<". $key. " ><! [cdata[]. $val. "] ></". $key." > "; }} $xml. = "</xml>"; return $xml; }//post request a Web site that requires a certificate function Curl_post_ssl ($url, $vars, $second =30, $aHeader =array ()) {$ch = Curl_init (); Time-out curl_setopt ($ch, Curlopt_timeout, $second); curl_setopt ($ch, Curlopt_returntransfer, 1); Set the agent here, if any. curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_ssl_verifypeer,false); curl_setopt ($ch, Curlopt_ssl_verifyhost,false); Cert and key belong to two. Pem files//Please ensure that your Libcurl version supports two-way authentication with a version higher than 7.20.1 curl_setopt ($ch, Curlopt_sslcert,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Apiclient_cert.pem '); curl_setopt ($ch, Curlopt_sslkey,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Apiclient_key.pem '); curl_setopt ($ch, Curlopt_cainfo,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Rootca.pem '); if (count ($aHeader) >= 1) {curl_setopt ($ch, Curlopt_httpheader, $aHeader); } curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $vars); $data = curl_exec ($ch); if ($data) {curl_close ($ch); return $data; } else {$error = Curl_errno ($ch); echo "Call Faild, ErrorCode: $error \ n"; Curl_close ($ch); return false; } }}
Certificate file:
Please-api secure download in the background of the merchant, PHP please use the PHP certificate file
Red Envelopes Call Test files:
<?phprequire ' wxpay.class.php '; <br><br>//array parameter $money = 100; Minimum 1 yuan, unit sub $sender = "Morning Edge property"; $obj 2 = Array (); $obj 2[' wxappid '] = ""; appid$obj2[' mch_id '] = ""; Merchant id$obj2[' Mch_billno ' = "Merchant id". Date (' Ymdhis '). Rand (1000,9999); Combined into 28-bit, according to official development documents, you can set $obj2[' client_ip ' = $_server[' remote_addr ']; $obj 2[' re_openid '] = " Ouxo6v26wdymhthd9kfdw6nb_xbe "; Receive red Envelopes openid$obj2[' total_amount ' = $money; $obj 2[' min_value '] = $money; $obj 2[' max_value '] = $money; $obj 2[' Total_num ' ] = 1; $obj 2[' nick_name ' = $sender; $obj 2[' send_name '] = $sender; $obj 2[' wishing '] = "Congratulations and fortune"; $obj 2[' act_name ') = $sender. " Red envelopes "; $obj 2[' remark ') = $sender." Red envelopes "; $url =" Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack "; $wxpay = new Wxpay (); $res = $wxpay->pay ($ URL, $obj 2); Var_dump ($res);
Related recommendations:
PHP implementation of the public number development of red envelopes method
JS implementation of Red envelope random algorithm (with code)
PHP using parabolic model to implement red envelope generation algorithm program source code