I believe everyone knows about the red envelope function. But if we want to integrate the red envelope into the website, what should we do? The following small series will share with you a php red packet interface test example. I hope the article will help you with the red packet function. I believe all of you know, but what should we do if we want to integrate red packets into the website? The following small series will share with you a php red packet interface test example. I hope the article will help you.
Script ec (2); script
The following is a class. Usage:
$ Arr ['openid'] = 'ojgttt8of9vdycgsjmachpa-jy1U ';
$ Arr ['hbname'] = "withdrawal application ";
$ Arr ['body'] = "your withdrawal application has been successful ";
$ Arr ['region'] = 1;
$ Comm = new Common_util_pub ();
$ Re = $ comm-> sendhongbaoto ($ arr );
Var_dump ($ re );
Note that the certificate location and the key set in the merchant's background must be modified.
Header ("Content-type: text/html; charset = UTF-8 ");
Class Common_util_pub
{
/**
* Hbname: red envelope name; Amount of the red envelope/The openid user ID of the Yuan body content
* @ Param undefined $ arr
*
* @ Return
*/
Public function sendhongbaoto ($ arr ){
// $ Comm = new Common_util_pub ();
$ Data ['mch _ id'] = '000000 ';
$ Data ['mch _ billno'] = '000000 '. date ("Ymd", time ()). date ("His", time ()). rand (1111,9999 );
$ Data ['nonce _ str'] = self: createNoncestr ();
$ Data ['re _ openid'] = $ arr ['openid'];
$ Data ['wxappid '] = 'wx8axxxxxbac4905 ';
$ Data ['Nick _ name'] = $ arr ['hbname'];
$ Data ['send _ name'] = $ arr ['hbname'];
$ Data ['total _ amount '] = $ arr ['authorization'] * 100;
$ Data ['min _ value'] = $ arr ['duration'] * 100;
$ Data ['max _ value'] = $ arr ['value'] * 100;
$ Data ['total _ num'] = 1;
$ Data ['client _ ip'] = $ _ SERVER ['remote _ ADDR '];
$ Data ['act _ name'] = 'test activity ';
$ Data ['remark'] = 'note ';
$ Data ['wishing'] = $ arr ['body'];
If (! $ Data ['re _ openid']) {
$ Rearr ['Return _ msg '] = 'user openid' missing ';
Return $ rearr;
}
$ Data ['sign'] = self: getSign ($ data );
$ Xml = self: arrayToXml ($ data );
// Var_dump ($ xml );
$ Url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ";
$ Re = self: wxHttpsRequestPem ($ xml, $ url );
$ Rearr = self: xmlToArray ($ re );
Return $ rearr;
}
Function trimString ($ value)
{
$ Ret = null;
If (null! = $ Value)
{
$ Ret = $ value;
If (strlen ($ ret) = 0)
{
$ Ret = null;
}
}
Return $ ret;
}
/**
* Function: generate a random string of no more than 32 characters
*/
Public function createNoncestr ($ length = 32)
{
$ Chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";
$ Str = "";
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Str. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1 );
}
Return $ str;
}
/**
* Function: format parameters. The signature process must be
*/
Function formatBizQueryParaMap ($ paraMap, $ urlencode)
{
$ Buff = "";
Ksort ($ paraMap );
Foreach ($ paraMap as $ k => $ v)
{
If ($ urlencode)
{
$ V = urlencode ($ v );
}
// $ Buff. = strtolower ($ k). "=". $ v ."&";
$ Buff. = $ k. "=". $ v ."&";
}
$ ReqPar;
If (strlen ($ buff)> 0)
{
$ ReqPar = substr ($ buff, 0, strlen ($ buff)-1 );
}
Return $ reqPar;
}
/**
* Function: generate a signature.
*/
Public function getSign ($ Obj)
{
Foreach ($ Obj as $ k => $ v)
{
$ Parameters [$ k] = $ v;
}
// Signature Step 1: Sort parameters in Lexicographic Order
Ksort ($ Parameters );
$ String = $ this-> formatBizQueryParaMap ($ Parameters, false );
// Echo '[string1]'. $ String .'
';
// Signature Step 2: add the KEY after the string
$ String = $ String. "& key =". "254554sefg4exxxxxxxxs5cds1"; // key set by the merchant background
// Echo "[string2]". $ String ."
";
// Signature Step 3: MD5 Encryption
$ String = md5 ($ String );
// Echo "[string3]". $ String ."
";
// Signature Step 4: Convert all characters into uppercase letters
$ Result _ = strtoupper ($ String );
// Echo "[result]". $ result _."
";
Return $ result _;
}
/**
* Function: Convert array to xml
*/
Public function arrayToXml ($ arr)
{
$ Xml =" ";
Foreach ($ arr as $ key => $ val)
{
If (is_numeric ($ val ))
{
$ Xml. = "<". $ key. ">". $ val ." ";
}
Else
$ Xml. = "<". $ key. ">".$val." ";
}
$ Xml. =" ";
Return $ xml;
}
/**
* Function: Convert xml into array
*/
Public function xmlToArray ($ xml)
{
// Convert XML into array
$ Array_data = json_decode (json_encode (simplexml_load_string ($ xml, 'simplexmlelement', LIBXML_NOCDATA), true );
Return $ array_data;
}
Public function wxHttpsRequestPem ($ vars, $ url, $ second = 30, $ aHeader = array ()){
$ Ch = curl_init ();
// Timeout
Curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// Set proxy here, if any
// Curl_setopt ($ ch, CURLOPT_PROXY, '10. 206.30.98 ');
// Curl_setopt ($ ch, CURLOPT_PROXYPORT, 8080 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
// Select either of the following methods
// Method 1. The cert and key belong to two. pem files respectively.
// The default format is PEM, which can be commented out.
Curl_setopt ($ ch, CURLOPT_SSLCERTTYPE, 'pem ');
Curl_setopt ($ ch, CURLOPT_SSLCERT, dirname (_ FILE _). '/hongbao/apiclient_cert.pem ');
// The default format is PEM, which can be commented out.
Curl_setopt ($ ch, CURLOPT_SSLKEYTYPE, 'pem ');
Curl_setopt ($ ch, CURLOPT_SSLKEY, dirname (_ FILE _). '/hongbao/apiclient_key.pem ');
Curl_setopt ($ ch, CURLOPT_CAINFO, 'pem ');
Curl_setopt ($ ch, CURLOPT_CAINFO, dirname (_ FILE _). '/hongbao/rootca. pem ');
// Method 2: combine two files into A. pem File
// Curl_setopt ($ ch, CURLOPT_SSLCERT, getcwd (). '/all. 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;
}
}
}
?>