Php version of WeChat public account interface to realize the red envelope method, php red envelope

Source: Internet
Author: User
Tags openid

Php public account interface for red packet sending, php red packet

This article describes how to use the php public account interface to send red packets. We will share this with you for your reference. The details are as follows:

A recent task needs to be used to automatically send red packets to users. To complete this task, you need to have some materials

Merchant ID, applied for payment

Public Account under the subject of the merchant ID

Let's take a look.

You only need to complete the following steps.

Call the red envelope code on the public account Server

/*** Red packet function */public function sendredpack () {$ re_openid = $ this-> _ pg ('re _ openid'); $ inputObj = new sendredpack_pub (); if (! $ Re_openid) {return "red envelope function, the user of the red envelope cannot be blank";} $ inputObj-> setParameter ('re _ openid', $ re_openid ); // openid $ inputObj-> setParameter ('send _ name', "auto parts shop 1") of the user receiving the red packet "); // The Name Of The red packet sender $ inputObj-> setParameter ('total _ amount', "100"); // The amount of the user who receives the red packet, precise to $ inputObj-> setParameter ('total _ num', "1"); // number of red packets received $ inputObj-> setParameter ('wishing', "birthday fortune, thank you for your support. "); // your openid $ inputObj-> setParameter ('client _ ip'," 121.40.157.243 "); // call the interface IP $ inputObj-> setParameter ('act _ name', "Xiao Deng Thanksgiving red packet"); // red packet subject $ inputObj-> setParameter ('remark ', "Thank you for your support"); // remarks $ response = $ inputObj-> getResult (); return $ response ;}

Add a class to the payment auxiliary tool layer to complete the red envelope function

/*** Red packet sending interface **/class sendredpack_pub extends Wxpay_client_pub {function _ construct () {// set the interface link $ this-> url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack "; // set the curl timeout time $ this-> curl_timeout = WxPayConf_pub: CURL_TIMEOUT;}/*** generate the interface parameter xml */function createXml () {try {$ this-> parameters ["mch_billno"] = WxPayConf_pub: MCHID. createUnique (); // merchant's Order Number $ this-> parameters ["wxappid"] = WxPayConf_pub: APPID; // public account ID $ this-> parameters ["mch_id"] = WxPayConf_pub: MCHID; // merchant ID $ this-> parameters ["nonce_str"] = $ this-> createNoncestr (); // random string $ this-> parameters ["sign"] = $ this-> getSign ($ this-> parameters ); // signature return $ this-> arrayToXml ($ this-> parameters);} catch (SDKRuntimeException $ e) {die ($ e-> errorMessage ());}} /*** function: obtain the result and use the certificate to communicate */function getResult () {$ this-> postXmlSSL (); $ this-> result = $ this-> xmlToArray ($ this-> response); return $ this-> result ;}}

Then deploy the pay-as-you-go service !! Then add a "red envelope" to the service of the public account ". The above results can be achieved.

Develop Based on the document

Please carefully read the interface documentation and refer to the documentation for development. Please note that, to ensure the security of merchant funds, the interface strongly verifies the binding relationship between the merchant ID and the appid, and the correspondence between appid and openid (if there is no binding relationship between the merchant ID and appid, that is, the appid does not apply for payment or the applied payment merchant ID does not match, or the openid belongs to appid is incorrect, ).

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.