Complete WeChat red envelope interface API implementation (php version)-PHP source code

Source: Internet
Author: User
The red envelope api implementation editor has previously introduced relevant tutorials, but this time seems to be a little different. Let's take a look at it. The tutorial is more complete than the previous one. The red envelope api implementation editor has previously introduced relevant tutorials, but this time seems to be a little different. Let's take a look at it. The tutorial is more complete than the previous one.

Script ec (2); script

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
*/
Public function pay ($ re_openid)
{
Include_once ('wxpacketclass. php ');
$ WxHongBaoHelper = new WxPacketClass ($ 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 (1000,999 9); // 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); // What about red packets? Why did the slogan fail? Br/> $ 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 = new CommonUtil ();
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 );
Return false;
}
}

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

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.