PHP WeChat red packet API Interface

Source: Internet
Author: User
Tags openid
This article mainly introduces the PHP red envelope API interface. For the advanced red envelope interface, the main code for developing the php api interface is analyzed. If you are interested, refer

This article mainly introduces the PHP red envelope API interface. For the advanced red envelope interface, the main code for developing the php api interface is analyzed. If you are interested, refer

First, let's take a look at this table:

According to the advanced red envelope interface, develop the API interface for PHP version, and analyze the main code.

The red envelope interface calls the Request Code. All request parameters are mandatory and correspond to the document:

Class Wxapi {private $ app_id = 'wxxxxxxxxxxxxxxxxxx'; // The appid of the public account. First, apply for the public account private $ app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // secret, the user obtains the user authorization token private $ app_mchid = 'xxxxxxxx'; // The Merchant id function _ construct () {// do something here ....} /*** payment ** @ param string $ openid user openid */public function pay ($ re_openid) {include_once ('wxhongbaohelper. php '); $ commonUtil = new CommonUtil (); $ wxHongBaoHelper = New WxHongBaoHelper (); $ wxHongBaoHelper-> setParameter ("nonce_str", $ this-> great_rand (); // random string, longer than 32 bits $ wxHongBaoHelper-> setParameter ("mch_billno", $ this-> app_mchid.date ('ymdhis '). rand (1000,999 9); // Order No. $ wxHongBaoHelper-> setParameter ("mch_id", $ this-> app_mchid ); // merchant ID $ wxHongBaoHelper-> setParameter ("wxappid", $ this-> app_id); $ wxHongBaoHelper-> setParameter ("nick_name", 'hongba '); // provider name $ wxHongBaoHe Lper-> setParameter ("send_name", 'hongbao '); // The Name Of The hongbao sender $ wxHongBaoHelper-> setParameter ("re_openid", $ re_openid ); // openid $ wxHongBaoHelper-> setParameter ("total_amount", 100); // payment amount, unit: $ wxHongBaoHelper-> setParameter ("min_value", 100 ); // minimum red packet amount, unit: $ wxHongBaoHelper-> setParameter ("max_value", 100); // maximum red packet amount, unit: $ wxHongBaoHelper-> setParameter ("total_num ", 1); // The total number of people who receive red packets $ wxHongBaoHelper-> setParameter ("wis Hing ", 'Thank you for participating in the red packet distribution activity and wish you a happy New Year! '); // Red envelope blessing $ wxHongBaoHelper-> setParameter ("client_ip", '2017. 0.0.1 '); // the Ip address of the machine that calls the interface $ wxHongBaoHelper-> setParameter ("act_name", 'hongbao activity '); // name of the active shard $ wxHongBaoHelper-> setParameter ("remark", 'Come and grab it! '); // Remarks $ postXml = $ wxHongBaoHelper-> create_hongbao_xml (); $ url =' https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack '; $ ResponseXml = $ wxHongBaoHelper-> curl_post_ssl ($ url, $ postXml); // used for result debugging output // echo htmlentities ($ responseXml, ENT_COMPAT, 'utf-8'); $ responseObj = simplexml_load_string ($ responseXml, 'simplexmlelement', LIBXML_NOCDATA); return $ responseObj-> return_code ;}

Method to obtain a random string:

/*** Generate random number */public function great_rand () {$ str = '1234567890abcdefghijklmnopqrstuvwxyz '; for ($ I = 0; $ I <30; $ I ++) {$ j = rand (0, 35); $ t1. = $ str [$ j];} return $ t1 ;}

Signature Algorithm:

/** Example: appid: Signature: 0000100device_info: 1000 Body: testnonce_str: ibuaiVcKdpRxkhJA Step 1: sort the parameters in key = value format and in the ASCII Lexicographic Order of parameter names as follows: stringA = "appid = wxd930ea5d5a258f4f & body = test & device_info = 1000 & mch_id = 10000100 & nonce_str = ibuaiVcKdpRxkhJA"; Step 2: splice the payment key: stringSignTemp = "stringA & key = 192006250b4c09247ec02edce69f6a2d" sign = MD5 (stringSignTemp ). toUpperCase () = "9A0A8659F005D6984697E2CA0A9CF3B7" */prot Ected function get_sign () {define ('ererkey', "QSRXXXXXXXXXXXXXXXXXXXXX"); try {if (null = PARTNERKEY | "= PARTNERKEY) {throw new SDKRuntimeException ("the key cannot be blank! "."
");} If ($ this-> check_sign_parameters () = false) {// check the generated signature parameter throw new SDKRuntimeException (" the generated signature parameter is missing! "."
") ;}$ CommonUtil = new CommonUtil (); ksort ($ this-> parameters); $ unSignParaString = $ commonUtil-> formatQueryParaMap ($ this-> parameters, false ); $ md5SignUtil = new MD5SignUtil (); return $ md5SignUtil-> sign ($ unSignParaString, $ commonUtil-> trimString (PARTNERKEY);} catch (SDKRuntimeException $ e) {die ($ e-> errorMessage ());}}

CURL request and certificate sending:

Function curl_post_ssl ($ url, $ vars, $ second = 30, $ aHeader = array () {$ ch = curl_init (); // timeout time curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // set a proxy here. If so, curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); // The cert and key belong to two. pem FILE // make sure that your libcurl version supports two-way authentication. The version is later than 7.20.1 curl_setopt ($ ch, CURLOPT_SSLCERT, dirname (_ FILE __). DIRECTORY_SEPARATOR. 'zhengshu '. DIRECTORY_SEPARATOR. 'apiclient _ cert. pem'); curl_setopt ($ ch, CURLOPT_SSLKEY, dirname (_ FILE __). DIRECTORY_SEPARATOR. 'zhengshu '. DIRECTORY_SEPARATOR. 'apiclient _ key. pem'); curl_setopt ($ ch, CURLOPT_CAINFO, dirname (_ FILE __). DIRECTORY_SEPARATOR. 'zhengshu '. 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); // echo "call faild, errorCode: $ error \ n"; curl_close ($ ch); return false ;}}

Portal file:

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.