This article is an example of the PHP version of the micro-letter red Envelope interface usage. Share to everyone for your reference, specific as follows:
The function of micro-letter Red Envelopes I believe all my friends know, but if we want to integrate the red envelopes to the website, then how to do it? Here is a small set to share a PHP micro-letter Red Envelopes Interface Test example, I hope the article can help you friends
Here is a class, using the method:
$arr [' OpenID ']= ' ojgttt8of9vdycgsjmachpa-jy1u ';
$arr [' Hbname ']=] "application for submission";
$arr [' Body ']= ' your present application has been successful];
$arr [' Fee ']=1;
$comm = new Common_util_pub ();
$re = $comm->sendhongbaoto ($arr);
Var_dump ($re);
Note the certificate location and key for the merchant backend settings need to be modified.
<?php header ("content-type:text/html;
Charset=utf-8 "); Class Common_util_pub {/** * hbname red envelope name fee red Envelope amount/yuan body content OpenID Micro User ID * @param undefined $arr * * @return * * * publi C function Sendhongbaoto ($arr) {//$comm = new Common_util_pub (); $data [' mch_id '] = ' 120005402 '; $data [' mch_billno '] = ' 120
005402 '. Date ("Ymd", Time ()). Date ("He", 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 [' Fee ']*100;
$data [' min_value '] = $arr [' Fee ']*100;
$data [' max_value '] = $arr [' Fee ']*100;
$data [' total_num '] = 1;
$data [' client_ip '] = $_server[' remote_addr '];
$data [' act_name '] = ' test activity ';
$data [' remark '] = ' remark ';
$data [' wishing '] = $arr [' body '];
if (! $data [' Re_openid ']) {$rearr [' return_msg ']= ' lack of user OpenID ';
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; /** * Action: Generate random string, not longer than 32 bits/public function createnoncestr ($length =) {$chars = "ABCDEFGHIJKLMNOPQR
stuvwxyz0123456789 ";
$str = "";
for ($i = 0; $i < $length; $i + +) {$str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
return $str;
/** * Action: Format parameters, the signature process requires the use of the */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; /** * Action: Generate signature/Public function getsign ($OBJ) {foreach ($Obj as $k => $v) {$Parameters [$
K] = $v;
//Signature Step one: Sort parameters by Dictionary order ksort ($Parameters);
$String = $this->formatbizqueryparamap ($Parameters, false); echo ' string1 '. $String. '
</br> '; Signature Step Two: Add key $String = $String after String. &key= "." 254554sefg4exxxxxxxxs5cds1 "; Merchant Background Set key//echo "" string2 "". $String. "
</br> ";
Signature Step Three: MD5 encryption $String = MD5 ($String); echo "" String3 "". $String. "
</br> ";
Signature Step four: All characters are converted to uppercase $result _ = Strtoupper ($String); echo "" Result "". $result _.
</br> ";
return $result _;
/** * Action: Array to XML/Public 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; /** * Action: Convert XML to array/Public function Xmltoarray ($xml) {//Convert XML to array $array _data = Json_decode (j
Son_encode (simplexml_load_string ($xml, ' simplexmlelement ', Libxml_nocdata)), true);
return $array _data;
The Public Function Wxhttpsrequestpem ($vars, $url, $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_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);
The following two ways to select a//first method, cert and key belong to two. Pem File//The default format is PEM, you can comment 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 annotated 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 '); The second way, two files synthesize 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;
}}}?>
For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.