: This article mainly introduces the API implementation of the red envelope interface (php version). If you are interested in the PHP Tutorial, please refer to it. More please support: http://www.webyang.net/Html/web/article_245.html
I. red envelope document description
Currently, red packets are divided into two types: cash red packets and fission red packets.
1, cash red envelope: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php? Chapter = 13_5
2, fission red envelope: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php? Chapter = 16_5
For more information, see: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php
II. php interface implementation
This article explains how to call a cash red envelope. The rest is basically the same, so I will not try it.
Parameter description:
Code implementation:
Fragment 1,
- /**
- * Payment
- * @ Param string $ openid: User openid
- */
- Publicfunction pay ($ re_openid)
- {
- Include_once ('wxpacketclass. php ');
- $ WxHongBaoHelper = newWxPacketClass ($ this-> app_sign );
- $ WxHongBaoHelper-> setParameter ("nonce_str", $ this-> great_rand (); // random string, not longer than 32 characters
- $ WxHongBaoHelper-> setParameter ("mch_billno", $ this-> app_mchid.date ('ymdhis '). rand (); // Order No. (28 bits)
- $ WxHongBaoHelper-> setParameter ("mch_id", $ this-> app_mchid); // merchant ID
- $ WxHongBaoHelper-> setParameter ("wxappid", $ this-> app_id );
- $ WxHongBaoHelper-> setParameter ("send_name", 'Yang Hehong Tech '); // name of the Red Packet Sender
- $ WxHongBaoHelper-> setParameter ("re_openid", $ re_openid); // openid
- $ WxHongBaoHelper-> setParameter ("total_amount", 100); // payment amount, in minutes
- $ WxHongBaoHelper-> setParameter ("total_num", 1); // total number of recipients of red packets
- $ WxHongBaoHelper-> setParameter ("wishing", 'you will be happy in your later years! '); // Red envelope blessing
- $ WxHongBaoHelper-> setParameter ("client_ip", '192. 0.0.1 '); // Ip address of the machine that calls the interface
- $ WxHongBaoHelper-> setParameter ("act_name", 'New Year's red packet activity'); // name of the active token
- $ WxHongBaoHelper-> setParameter ("remark", 'Come and grab it! '); // Remarks
- $ PostXml = $ wxHongBaoHelper-> create_hongbao_xml ();
- $ Url = 'https: // api.mch.weixin.qq.com/mmp aymkttransfers/sendredpack ';
- $ ResponseXml = $ wxHongBaoHelper-> curl_post_ssl ($ url, $ postXml );
- $ ResponseObj = simplexml_load_string ($ responseXml, 'simplexmlelement', LIBXML_NOCDATA );
- Return $ responseObj-> return_code;
- }
Part 2,
- // Generate XML information for the red envelope interface
- /*
-
- ! [CDATA [E1EE61A9]
- ! [CDATA [00100]
- ! [CDATA [888]
- ! [CDATA [wxcbda96de0b165486]
- ! [CDATA [send_name]
- ! [CDATA [onqOjjXXXXXXXXX]
- ! [CDATA [100]
- ! [CDATA [1]
- ! [CDATA [getting lucky]
- ! [CDATA [127.0.0.1]
- ! [CDATA [New Year red envelopes]
- ! [CDATA [act_id]
- ! [CDATA [New Year red envelopes]
- */
- Function create_hongbao_xml ($ retcode = 0, $ reterrmsg = "OK "){
- Try {
- $ This-> setParameter ('sign', $ this-> get_sign ());
- $ CommonUtil = newCommonUtil ();
- Return $ commonUtil-> arrayToXml ($ this-> parameters );
- } Catch (SDKException $ e ){
- Die ($ e-> errorMessage ());
- }
- }
Part 3,
- Function curl_post_ssl ($ url, $ vars, $ 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_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
- Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
- // Cert and key belong to two. pem files respectively.
- Curl_setopt ($ ch, CURLOPT_SSLCERT, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. DIRECTORY_SEPARATOR. 'apiclient _ cert. pem ');
- Curl_setopt ($ ch, CURLOPT_SSLKEY, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. DIRECTORY_SEPARATOR. 'apiclient _ key. pem ');
- Curl_setopt ($ ch, CURLOPT_CAINFO, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. 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 );
- Curl_close ($ ch );
- Returnfalse;
- }
- }
Code structure:
| ~ Action/
| '-PacketClass. php
| ~ Lib/
| ~ Cert/
|-Apiclient_cert.pem
|-Apiclient_key.pem
| '-Rootca. pem
|-SdkExtraClass. php
|-WxApi. php
| '-WxPacketClass. php
'-Index. php
Each file has a detailed description.
III. effect display
If you need source code, contact me ~
The above introduces the red envelope interface API implementation (php version), including the content, hope to be helpful to friends who are interested in PHP tutorials.