First, let's take a look at this form:
According to the advanced Red envelope interface, the development of the PHP version of the API interface, now the main code analysis.
The red envelope interface calls the request code, and all the request parameters are required parameters corresponding to the document:
Class Wxapi {private $app _id = ' wxxxxxxxxxxxxx ';//Public account AppID, first apply for the supporting public account private $app _secret = ' xxxxxxxxxxxxxxxxxxxxxx XX ';//public number secret, the user obtains the user authorization token private $app _mchid = ' XXXXXXXX ';//merchant ID function __construct () {//do sth here ...}/** * Branch * @param string $openid user OpenID */Public Function Pay ($re _openid) {include_once (' wxhongbaohelper.php '); $commonUtil = new Commonutil (); $wxHongBaoHelper = new Wxhongbaohelper (); $wxHongBaoHelper->setparameter ("Nonce_str", $this->great_rand ());//random string, not longer than 32-bit $wxHongBaoHelper Setparameter ("Mch_billno", $this->app_mchid.date (' Ymdhis '). Rand (1000, 9999));//Order number $wxHongBaoHelper Setparameter ("mch_id", $this->app_mchid);//Merchant number $wxHongBaoHelper->setparameter ("Wxappid", $this->app_id); $wxHongBaoHelper->setparameter ("Nick_name", ' red envelopes ');//Provider name $wxHongBaoHelper->setparameter ("Send_name", ' Red envelopes ') ;//The name of the sender of the red envelope $wxHongBaoHelper->setparameter ("Re_openid", $re _openid);//OpenID relative to the medical pulse $wxHongBaoHelper SetparametER ("Total_amount", 100);//payment amount, Unit $wxHongBaoHelper->setparameter ("Min_value", 100);//minimum red envelope amount, unit of $ Wxhongbaohelper->setparameter ("Max_value", 100);//maximum amount of red envelopes, Unit $wxHongBaoHelper->setparameter ("Total_num", 1) ;//Red envelope Anticipation total number of people $wxHongBaoHelper->setparameter ("Wishing", ' Thank you for your participation in the red envelopes distribution activities, I wish you a happy New Year! '///Red Envelopes Blessing astonished $wxHongBaoHelper->setparameter ("Client_ip", ' 127.0.0.1 ');//the Machine IP address of the calling interface $wxHongBaoHelper- Setparameter ("Act_name", "red envelope Activity"),//Live name $wxHongBaoHelper->setparameter ("Remark", ' Come and rob! ')///Note information $postXml = $wxHongBaoHelper->create_hongbao_xml (); $url = ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack '; $RESPONSEXML = $wxHongBaoHelper->curl_post_ssl ($url, $postXml); Debug output as result//echo htmlentities ($responseXml, Ent_compat, ' UTF-8 '); $RESPONSEOBJ = simplexml_load_string ($responseXml, ' simplexmlelement ', libxml_nocdata); return $RESPONSEOBJ->return_code; }
Get the random string method:
/** * Generate random number */public function Great_rand () {$str = ' 1234567890abcdefghijklmnopqrstuvwxyz ', for ($i =0; $i <30; $i + +) { $j =rand (0,35); $t 1. = $str [$j]; } return $t 1; }
Signature Algorithm:
/** Example: Appid:wxd111665abv58f4fmch_id:10000100device_info:1000body:testnonce_str:ibuaivckdprxkhja first step: on the parameters according to key= The format of value, sorted by the ASCII dictionary order of the parameter names as follows: Stringa= "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id= 10000100&nonce_str=ibuaivckdprxkhja "; Step two: Stitching payment key: stringsigntemp=" stringa&key= 192006250b4c09247ec02edce69f6a2d "Sign=md5 (stringsigntemp). toUpperCase () =" 9A0A8659F005D6984697E2CA0A9CF3B7 "*/ protected function Get_sign () {define (' Partnerkey ', "qsrxxxxxxxxxxxxxxxxxxxxx"); try {if (null = = Partnerkey | | "= = Partnerkey) {throw new Sdkruntimeexception (" The key cannot be empty! " . "
"); } if ($this->check_sign_parameters () = = False) {//Check generate signature parameter throw new Sdkruntimeexception ("Generate Signature parameter missing! " . "
"); } $commonUtil = new Commonutil (); Ksort ($this->parameters); $unSignParaString = $commonUtil->formatqueryparamap ($this->parameters, false); $MD 5SignUtil = new Md5signutil (); Return $MD 5signutil->sign ($unSignParaString, $commonUtil->trimstring (Partnerkey)); }catch (Sdkruntimeexception $e) {die ($e->errormessage ());}}
Curl requests and sends certificates:
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); Here set the agent, 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; }}
Entry file:
@require "pay.php";//get user Information $get = $_get[' param ']; $code = $_get[' code '];//determine if code exists if ($get = = ' Access_token ' & &!empty ($code)) {$param [' param '] = ' access_token '; $param [' code '] = $code; $packet = new Packet ();//Get user OpenID information $US Erinfo = $packet->_route (' userinfo ', $param); if (Empty ($userinfo [' OpenID ']) { exit ("Noauth")}//->_route (' Wxpacket ', Array (' OpenID ' =>$ userinfo[' OpenID '));} else{$packet->_route (' userinfo ');}
The above is about the PHP Red Envelopes API Interface detailed code, share to everyone, hope that everyone's learning has helped.