Home  >  Article  >  Backend Development  >  PHP WeChat public account interface implements the method of sending red envelopes

PHP WeChat public account interface implements the method of sending red envelopes

墨辰丷
墨辰丷Original
2018-06-01 09:32:472055browse

This article mainly introduces the method of sending red envelopes through the interface of the WeChat public account in the PHP version. It analyzes the interface calling method and related usage precautions in the PHP version of the WeChat public account in the form of examples. Friends in need can refer to the following

The example in this article describes the method of sending red envelopes through the PHP version of WeChat public account interface. I would like to share it with you for your reference. The details are as follows:

I recently received a task and needed to use WeChat to automatically send red envelopes to users. To complete this task, you need some materials

WeChat merchant account, and have applied for WeChat payment

WeChat public account under the main body of the WeChat merchant account

Let’s take a look at the renderings first

##Only need to complete the next few steps That's it.

Call the red envelope code on the WeChat public account server

/*
**微信红包功能
*/
public function sendredpack(){
 $re_openid = $this->_pg('re_openid');
  $inputObj = new sendredpack_pub();
if(!$re_openid){
  return "微信红包功能,收红包用户不能为空";
}
  $inputObj->setParameter('re_openid',$re_openid); //收红包的用户的openid
  $inputObj->setParameter('send_name',"汽配一号铺"); //红包发送者名称
  $inputObj->setParameter('total_amount',"100"); //收红包的用户的金额,精确到分
  $inputObj->setParameter('total_num',"1"); //收红包的个数
  $inputObj->setParameter('wishing',"恭喜发财,谢谢支持,小小心意"); //收红包的用户的openid
  $inputObj->setParameter('client_ip',"121.40.157.243"); //调用接口的IP
  $inputObj->setParameter('act_name',"小邓感恩红包"); //红包主题
  $inputObj->setParameter('remark',"谢谢大家一路一来的支持"); //备注
  $response = $inputObj->getResult();
  return $response;
}

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

/**
* 微信发红包接口
**/
class sendredpack_pub extends Wxpay_client_pub
{
   function __construct() {
  //设置接口链接
   $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
  //设置curl超时时间
  $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
}
/**
* 生成接口参数xml
*/
function createXml()
{
  try
 {
  $this->parameters["mch_billno"] = WxPayConf_pub::MCHID.createUnique();//商户订单号
  $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
  $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  return $this->arrayToXml($this->parameters);
 }catch (SDKRuntimeException $e) {
   die($e->errorMessage());
 }
}
/**
* 作用:获取结果,使用证书通信
*/
function getResult()
{
  $this->postXmlSSL();
  $this->result = $this->xmlToArray($this->response);
  return $this->result;
}
}

Then deploy the WeChat payment service, and that’s it! ! Then add "red envelopes" to the service of making a WeChat official account (this official account). The above effect can be achieved

Develop according to the document

Please read the interface document carefully and refer to the document for development. Please note that, In order to ensure the security of merchant funds, the interface strongly verifies the binding relationship between the merchant number and appid, as well as the corresponding relationship between appid and openid (if there is no binding relationship between the merchant number and appid, that is, the appid has not applied for WeChat payment or If the merchant number applied for WeChat payment does not match, or the openid belongs to the wrong appid, the interface will return an error).

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Using UUID in PHP framework Laravel to implement data table operations

##PHP mysql ajax to implement lightweight Method of level chat room


phpMethod of implementing cross-domain form submission

The above is the detailed content of PHP WeChat public account interface implements the method of sending red envelopes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn