This article is mainly to share with you PHP implementation of red Envelopes Code, hope to help everyone. WXHB Package yourself to the official website to download the latest, including certificates and so on, set up good access to IP, but also the most important you have applied for Success red envelope function, because the need to pay the water and 90 days, limited.
<?phpnamespace index\controller;use think\controller;use index\lib\hb\wxhb;class HbController extends Controller { Private $app _id = ""; Appidprivate $app _secret = ""; Appsecretprivate $app _mchid = ""; Merchant Number Private $api _key = ""; Merchant Payment key key////Send Red envelopes Public Function SENDHB () {//include_once ('./weixinredpacket/wxhongbaohelper.php ');/// New WXHB (); $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, 9 999)); Order number $wxhongbaohelper->setparameter ("mch_id", $this->app_mchid); Merchant number $wxhongbaohelper->setparameter ("Wxappid", $this->app_id); $wxHongBaoHelper->setparameter ("Send_ Name ", Chongqing Today"); Red Envelope sender name $wxhongbaohelper->setparameter ("Re_openid", $re _openid); Relative to the openid$wxhongbaohelper->setpar of medical vein ExchangeAmeter ("Total_amount", $price); Payment amount, Unit sub $wxhongbaohelper->setparameter ("Total_num", 1); The total number of red envelopes anticipation $wxhongbaohelper->setparameter ("Wishing", ' monkey Auspicious '); Red Envelopes Blessing Language $wxhongbaohelper->setparameter ("Client_ip", ' 219.153.65.50 '); The machine Ip address of the Calling interface $wxhongbaohelper->setparameter ("Act_name", ' re-reporting group '); Live name $wxhongbaohelper->setparameter ("remark", "Happy New Year!") ' ); Note Information//generate XML and generate signature $postxml = $wxHongBaoHelper->create_hongbao_xml ($this->api_key);//var_dump ($postXml);// exit;//Submit Request: ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack '; $responseXml = $wxHongBaoHelper- >curl_post_ssl ($url, $postXml); $responseObj = simplexml_load_string ($responseXml, ' simplexmlelement ', Libxml_ Nocdata);//convert array $responsearr = (array) $responseObj; $return _code = $responseArr [' Return_code ']; $result _code = $respon Searr [' Result_code '];//determines if the red packets are sent successfully if ($return _code = = "SUCCESS" && $result _code = "SUCCESS") {Dump ($response ARR); echo "SUCCESS";} else {echo 'Send Failed ";d UMP ($RESPONSEARR);}} Gets the random number private function Great_rand () {$str = ' 1234567890abcdefghijklmnopqrstuvwxyz '; for ($i = 0; $i < $i + +) {$j = R and (0, a); $t 1. = $str [$j];} return $t 1;}}