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,
- /**
- * 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 bits
- $wxHongBaoHelper - Setparameter ("Mch_billno", $this-app_mchid. Date (' ymdhis '). Rand (9999)); //Order number (28-bit)
- $wxHongBaoHelper - Setparameter ("mch_id", $this-app_mchid); //Merchant number
- $wxHongBaoHelper - Setparameter ("Wxappid", $this-app_id);
- $wxHongBaoHelper - Setparameter ("Send_name",' Yang and Acer technology '); //Red envelope sender name
- $wxHongBaoHelper - Setparameter ("Re_openid", $re _openid); //openid
- $wxHongBaoHelper - Setparameter ("Total_amount",+); //Payment amount, Unit division
- $wxHongBaoHelper - Setparameter ("Total_num",1); //Red Envelope Anticipation total number of people
- $wxHongBaoHelper - Setparameter ("Wishing","to your old age, I wish you a happy old age!") '); //Red Envelopes Blessing astonished
- $wxHongBaoHelper - Setparameter ("Client_ip",' 127.0.0.1 '); //Ip address of the machine calling the interface
- $wxHongBaoHelper - Setparameter ("Act_name","Red Envelopes Campaign"); //Live name
- $wxHongBaoHelper - Setparameter ("remark",' Everybody, come and rob! '); //Notes Information
- $postXml = $wxHongBaoHelper - Create_hongbao_xml ();
- $url = ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ' ;
- $responseXml = $wxHongBaoHelper - Curl_post_ssl ($url, $postXml);
- $RESPONSEOBJ = simplexml_load_string ($responseXml,' simplexmlelement ', libxml_nocdata );
- return $RESPONSEOBJ - Return_code ;
- }
Fragment II,
- Generate Red Envelope interface XML information
- /*
-
- ! [CDATA[E1EE61A9]]
- ! [CDATA[00100]]
- ! [cdata[888]]
- ! [cdata[wxcbda96de0b165486]]
- ! [Cdata[send_name]]
- ! [Cdata[onqojjxxxxxxxxx]]
- ! [CDATA[100]]
- ! [Cdata[1]]
- ! [cdata[congratulations on Fortune]]
- ! [cdata[127.0.0.1]]
- ! [cdata[New Year red Envelope]]
- ! [CDATA[ACT_ID]]
- ! [cdata[New Year red Envelope]]
- */
- 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());
- }
- }
Fragment III,
- function Curl_post_ssl ($url, $vars, $second= , $aHeader = Array ()){
- $ch = Curl_init ();
- Timeout period
- curl_setopt ($ch,curlopt_timeout,$second);
- curl_setopt ($ch,curlopt_returntransfer,1);
- Set the agent 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
- 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.
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.