PHP implementation WeChat red envelope code

Source: Internet
Author: User
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;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.