First, let's take a look at this form:
According to the micro-credit premium red envelope interface, the development of PHP version of the API interface, now for the main code analysis.
Red Packet Interface Call request code, all request parameters are required parameters corresponding to the document:
Class Wxapi {private $app _id = ' wxxxxxxxxxxxxx ';////Public account AppID, first apply for matching public account private $app _secret = ' xxxxxxxxxxxxxxxxxxxx
XXXX '//Public number secret, user obtains user authorization token private $app _mchid = ' XXXXXXXX ';//merchant ID function __construct () {//do sth here ...} /** * Micro Letter Payment * @param string $openid user OpenID/Public function Pay ($re _openid) {include_once (' WXHONGBAOHELPER.P
HP ');
$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 envelope");//Provider name $wxHongBaoHelper->setparameter ("Send_name", ' Red envelopes ') //The name of the red envelope sender $wxHongBaoHelper->setparameter ("Re_openid", $re _openid);//OpenID relative to the medical pulse $wxHoNgbaohelper->setparameter ("Total_amount", 100);//payment amount, Unit divided $wxHongBaoHelper->setparameter ("Min_value", 100) //Minimum Red envelope amount, Unit divided $wxHongBaoHelper->setparameter ("Max_value", 100);//maximum red envelope amount, Unit $wxHongBaoHelper->setparameter ( "Total_num", 1)//Red Envelopes Survival Total number $wxHongBaoHelper->setparameter ("Wishing", "Thank you for your participation in the red envelope 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!
//Memo Information $postXml = $wxHongBaoHelper->create_hongbao_xml ();
$url = ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ';
$RESPONSEXML = $wxHongBaoHelper->curl_post_ssl ($url, $postXml);
Use as result debug output//echo htmlentities ($responseXml, Ent_compat, ' UTF-8 ');
$RESPONSEOBJ = simplexml_load_string ($responseXml, ' simplexmlelement ', libxml_nocdata);
return $RESPONSEOBJ->return_code;
}
To get a 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:wxd111665abv58f4f mch_id:10000100 device_info:1000 body:test Nonce_str:ibuaivckdprxkhja The first step: to the parameters in accordance with the key= The format of value, sorted according to the ASCII Dictionary of parameter names, is as follows: Stringa= "Appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i d=
10000100&nonce_str=ibuaivckdprxkhja "; Step two: Stitching the payment key: stringsigntemp= "stringa&key=192006250b4c09247ec02edce69f6a2d" Sign=md5 (stringsigntemp). toUpperCase () = "9a0a8659f005d6984697e2ca0a 9cf3b7" */protected function get_sign () {define (' Partnerkey ', "
Qsrxxxxxxxxxxxxxxxxxxxxx "); try {if (null = Partnerkey | | "" = = Partnerkey) {throw new Sdkruntimeexception ("Key cannot be empty!") " . "
<br> "); } if ($this->check_sign_parameters () = = False) {//Check generate signature parameter throw new Sdkruntimeexception ("Generate Signature parameter missing!") " . "
<br> ");
} $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 Request and send certificate:
function Curl_post_ssl ($url, $vars, $second =30, $aHeader =array ()) {$ch = Curl_init ();
Timeout time 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, the version is 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 the code exists if
($get = = ' Access_token ' &&!empty ($code)) {
$param [' param '] = ' access_token ';
$param [' code '] = $code;
$packet = new Packet ();
Get user OpenID information
$userinfo = $packet->_route (' userinfo ', $param);
if (Empty ($userinfo [' OpenID '])) {
exit ("Noauth");
}
Method
of payment $packet->_route (' Wxpacket ', Array (' OpenID ' => $userinfo [' OpenID ']);
} else{
$packet->_route (' userinfo ');
}
The above is about the PHP micro-letter Red Envelopes API interface detailed code, to share with you, hope that the learning of everyone help.