WeChat Red Envelopes API Implementation (PHP version)

Source: Internet
Author: User
Tags cdata openid
Please support: http://www.webyang.net/Html/web/article_245.html

First, red Envelopes document description

At present, there are two kinds of red envelopes: cash red envelopes and fission red envelopes.
1, Cash red envelopes: Https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
2. Fission Red envelopes: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=16_5
See more: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php
Second, PHP interface implementation
This article explains the call of the cash red envelopes, the other roughly the same, not to try.
Parameter description:

Code implementation:
Fragment One,
 
 
  1. /**
  2. * Payment
  3. * @param string $openid user OpenID
  4. */
  5. Public function Pay ($re _openid)
  6. {
  7. include_once (' wxpacketclass.php ');
  8. $wxHongBaoHelper = New Wxpacketclass ($this,app_sign);
  9. $wxHongBaoHelper - Setparameter ("Nonce_str", $this-great_rand()); //random string, not longer than 32 bits
  10. $wxHongBaoHelper - Setparameter ("Mch_billno", $this-app_mchid. Date (' ymdhis '). Rand (9999)); //Order number (28-bit)
  11. $wxHongBaoHelper - Setparameter ("mch_id", $this-app_mchid); //Merchant number
  12. $wxHongBaoHelper - Setparameter ("Wxappid", $this-app_id);
  13. $wxHongBaoHelper - Setparameter ("Send_name",' Yang and Acer technology '); //Red envelope sender name
  14. $wxHongBaoHelper - Setparameter ("Re_openid", $re _openid); //openid
  15. $wxHongBaoHelper - Setparameter ("Total_amount",+); //Payment amount, Unit division
  16. $wxHongBaoHelper - Setparameter ("Total_num",1); //Red Envelope Anticipation total number of people
  17. $wxHongBaoHelper - Setparameter ("Wishing","to your old age, I wish you a happy old age!") '); //Red Envelopes Blessing astonished
  18. $wxHongBaoHelper - Setparameter ("Client_ip",' 127.0.0.1 '); //Ip address of the machine calling the interface
  19. $wxHongBaoHelper - Setparameter ("Act_name","Red Envelopes Campaign"); //Live name
  20. $wxHongBaoHelper - Setparameter ("remark",' Everybody, come and rob! '); //Notes Information
  21. $postXml = $wxHongBaoHelper - Create_hongbao_xml ();
  22. $url = ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ' ;
  23. $responseXml = $wxHongBaoHelper - Curl_post_ssl ($url, $postXml);
  24. $RESPONSEOBJ = simplexml_load_string ($responseXml,' simplexmlelement ', libxml_nocdata );
  25. return $RESPONSEOBJ - Return_code ;
  26. }
Fragment II,
 
 
  1. Generate Red Envelope interface XML information
  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[congratulations on Fortune]]
  12. ! [cdata[127.0.0.1]]
  13. ! [cdata[New Year red Envelope]]
  14. ! [CDATA[ACT_ID]]
  15. ! [cdata[New Year red Envelope]]
  16. */
  17. function Create_hongbao_xml ($retcode =0, $reterrmsg ="OK" ){
  18. Try {
  19. $this - Setparameter (' sign ', $this-get_sign());
  20. $commonUtil = New Commonutil ();
  21. return $commonUtil - Arraytoxml ($this,parameters);
  22. }catch(sdkexception $e){
  23. die ($e,errormessage());
  24. }
  25. }
Fragment III,
 
 
  1. function Curl_post_ssl ($url, $vars, $second= , $aHeader = Array ()){
  2. $ch = Curl_init ();
  3. Timeout period
  4. curl_setopt ($ch,curlopt_timeout,$second);
  5. curl_setopt ($ch,curlopt_returntransfer,1);
  6. Set the agent 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
  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. return false ;
  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.
Third, the effect shows

There is a need for source code can contact me ~

The above describes the Red Envelope API implementation (PHP version), including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.