WeChat red envelope interface API implementation (php version)

Source: Internet
Author: User
Tags openid
: 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,
 
 
  1. /**
  2. * Payment
  3. * @ Param string $ openid: User openid
  4. */
  5. Publicfunction pay ($ re_openid)
  6. {
  7. Include_once ('wxpacketclass. php ');
  8. $ WxHongBaoHelper = newWxPacketClass ($ this-> app_sign );
  9. $ WxHongBaoHelper-> setParameter ("nonce_str", $ this-> great_rand (); // random string, not longer than 32 characters
  10. $ WxHongBaoHelper-> setParameter ("mch_billno", $ this-> app_mchid.date ('ymdhis '). rand (); // Order No. (28 bits)
  11. $ WxHongBaoHelper-> setParameter ("mch_id", $ this-> app_mchid); // merchant ID
  12. $ WxHongBaoHelper-> setParameter ("wxappid", $ this-> app_id );
  13. $ WxHongBaoHelper-> setParameter ("send_name", 'Yang Hehong Tech '); // name of the Red Packet Sender
  14. $ WxHongBaoHelper-> setParameter ("re_openid", $ re_openid); // openid
  15. $ WxHongBaoHelper-> setParameter ("total_amount", 100); // payment amount, in minutes
  16. $ WxHongBaoHelper-> setParameter ("total_num", 1); // total number of recipients of red packets
  17. $ WxHongBaoHelper-> setParameter ("wishing", 'you will be happy in your later years! '); // Red envelope blessing
  18. $ WxHongBaoHelper-> setParameter ("client_ip", '192. 0.0.1 '); // Ip address of the machine that calls the interface
  19. $ WxHongBaoHelper-> setParameter ("act_name", 'New Year's red packet activity'); // name of the active token
  20. $ WxHongBaoHelper-> setParameter ("remark", 'Come and grab it! '); // Remarks
  21. $ PostXml = $ wxHongBaoHelper-> create_hongbao_xml ();
  22. $ Url = 'https: // api.mch.weixin.qq.com/mmp aymkttransfers/sendredpack ';
  23. $ ResponseXml = $ wxHongBaoHelper-> curl_post_ssl ($ url, $ postXml );
  24. $ ResponseObj = simplexml_load_string ($ responseXml, 'simplexmlelement', LIBXML_NOCDATA );
  25. Return $ responseObj-> return_code;
  26. }
Part 2,
 
 
  1. // Generate XML information for the red envelope interface
  2. /*
  3. ! [CDATA [E1EE61A9]
  4. ! [CDATA [00100]
  5. ! [CDATA [888]
  6. ! [CDATA [wxcbda96de0b165486]
  7. ! [CDATA [send_name]
  8. ! [CDATA [onqOjjXXXXXXXXX]
  9. ! [CDATA [100]
  10. ! [CDATA [1]
  11. ! [CDATA [getting lucky]
  12. ! [CDATA [127.0.0.1]
  13. ! [CDATA [New Year red envelopes]
  14. ! [CDATA [act_id]
  15. ! [CDATA [New Year red envelopes]
  16. */
  17. Function create_hongbao_xml ($ retcode = 0, $ reterrmsg = "OK "){
  18. Try {
  19. $ This-> setParameter ('sign', $ this-> get_sign ());
  20. $ CommonUtil = newCommonUtil ();
  21. Return $ commonUtil-> arrayToXml ($ this-> parameters );
  22. } Catch (SDKException $ e ){
  23. Die ($ e-> errorMessage ());
  24. }
  25. }
Part 3,
 
 
  1. Function curl_post_ssl ($ url, $ vars, $ second = 30, $ aHeader = array ()){
  2. $ Ch = curl_init ();
  3. // Timeout
  4. Curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second );
  5. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  6. // Set proxy here, if any
  7. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  8. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
  9. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
  10. // Cert and key belong to two. pem files respectively.
  11. Curl_setopt ($ ch, CURLOPT_SSLCERT, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. DIRECTORY_SEPARATOR. 'apiclient _ cert. pem ');
  12. Curl_setopt ($ ch, CURLOPT_SSLKEY, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. DIRECTORY_SEPARATOR. 'apiclient _ key. pem ');
  13. Curl_setopt ($ ch, CURLOPT_CAINFO, dirname (_ FILE _). DIRECTORY_SEPARATOR. 'cert'. DIRECTORY_SEPARATOR. 'rootca. pem ');
  14. If (count ($ aHeader)> = 1) curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ aHeader );
  15. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  16. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ vars );
  17. $ Data = curl_exec ($ ch );
  18. If ($ data ){
  19. Curl_close ($ ch );
  20. Return $ data;
  21. } Else {
  22. $ Error = curl_errno ($ ch );
  23. Curl_close ($ ch );
  24. Returnfalse;
  25. }
  26. }
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.

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.