search
HomeBackend DevelopmentPHP TutorialPHP WeChat official account randomly distributes cash red envelopes

This time I will bring you the function of php WeChat official account to randomly distribute cash red envelopes. What are the precautions for the function of php WeChat official account to randomly distribute cash red envelopes? The following is a practical case, let’s take a look.

The following development of cash red envelopes in WeChat payment refers to the WeChat payment development document, as shown in the figure below.

Cash red envelope is one of the marketing tools provided by the WeChat Pay merchant platform. It has been deeply loved by merchants and users since its launch. Merchants can issue cash red envelopes to WeChat Pay users through this platform. After the user receives the red envelope, the funds arrive in the user's WeChat Pay change account and have the same use exit as other funds in the change wallet; if the user does not receive the red envelope, the funds will be returned to the merchant's WeChat Pay account after 24 hours.

The following is the code snippet I used to test WeChat payment for sending red envelopes, for reference only.

/*测试微信企业给个人发红包*/ 
public function weixin_red_packet(){ 
  // 请求参数 
  // 随机字符串 
  $data['nonce_str']=$this->get_unique_value(); 
  //签名 
  $data['sign']=""; 
  //商户号,输入你的商户号 
  $data['mch_id']="**********"; 
  //商户订单号,可以按要求自己组合28位的商户订单号 
  $data['mch_billno']=$data['mch_id'].date("ymd")."888888".rand(1000,9999); 
  //公众帐号appid,输入自己的公众号appid 
  $data['wxappid']="*********"; 
  //商户名称 
  $data['send_name']="*******"; 
  //用户openid,输入待发红包的用户openid 
  $data['re_openid']="*********"; 
  //付款金额 
  $data['total_amount']="100"; 
  //红包发放总人数 
  $data['total_num']="1"; 
  //红包祝福语 
  $data['wishing']="********"; 
  //IP地址 
  $data['client_ip']=$_SERVER['LOCAL_ADDR']; 
  //活动名称 
  $data['act_name']="*******"; 
  //备注 
  $data['remark']="没有备注"; 
  //场景ID 
  // $data['send_id']="PRODUCT_5"; 
  //活动信息 
  // $data['risk_info']=""; 
  // 生成签名 
  //对数据数组进行处理 
  //API密钥,输入自己的appsecret 
  $appsecret="*********"; 
  $data=array_filter($data); 
  ksort($data); 
  $str=""; 
  foreach($data as $k=>$v){ 
    $str.=$k."=".$v."&"; 
  } 
  $str.="key=".$appsecret; 
  $data['sign']=strtoupper(MD5($str)); 
  /* 
    发红包操作: 
      1.将请求数据转换成xml 
      2.发送请求 
      3.将请求结果转换为数组 
      4.将请求信息和请求结果录入到数据库中 
      4.判断是否通信成功 
      5.判断是否转账成功 
   */ 
  //发红包接口地址 
  $url="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; 
  //将请求数据由数组转换成xml 
  $xml=$this->arraytoxml($data); 
  //进行请求操作 
  $res=$this->curl($xml,$url); 
  //将请求结果由xml转换成数组 
  $arr=$this->xmltoarray($res); 
  //请请求信息和请求结果录入到数据库中 
  // $transfer['partner_trade_no']=$data['partner_trade_no']; 
  // $transfer['request_data']=serialize($data);   
  // $transfer['response_data']=serialize($arr); 
  // if($arr['return_code']=="SUCCESS" && $arr['result_code']=="SUCCESS"){ 
  // $transfer['success']=1; 
  // $transfer_res['success']=1; 
  // }else{ 
  // $transfer['success']=0; 
  // $transfer_res['success']=0; 
  // $transfer_res['desc']=$arr['return_msg']; 
  // } 
  // $transfer['add_time']=time(); 
  // D("weixin_transfer")->add($transfer); 
  // 输出请求结果数组 
  echo "<pre class="brush:php;toolbar:false">"; 
  print_r($arr); 
  exit("weixin_red_packet"); 
} 
// 生成32位唯一随机字符串 
private function get_unique_value(){ 
  $str=uniqid(mt_rand(),1); 
  $str=sha1($str); 
  return md5($str); 
} 
// 将数组转换成xml 
private function arraytoxml($arr){ 
  $xml="<xml>"; 
  foreach($arr as $k=>$v){ 
    $xml.="<".$k.">".$v."</".$k.">"; 
  } 
  $xml.="</xml>"; 
  return $xml; 
} 
// 将xml转换成数组 
private function xmltoarray($xml){ 
  //禁止引用外部xml实体 
  libxml_disable_entity_loader(true); 
  $xmlstring=simplexml_load_string($xml,"SimpleXMLElement",LIBXML_NOCDATA); 
  $arr=json_decode(json_encode($xmlstring),true); 
  return $arr; 
} 
//进行curl操作 
private function curl($param="",$url) { 
  $postUrl = $url; 
  $curlPost = $param; 
  //初始化curl 
  $ch = curl_init();                    
  //抓取指定网页 
  curl_setopt($ch, CURLOPT_URL,$postUrl);          
  //设置header 
  curl_setopt($ch, CURLOPT_HEADER, 0);           
  //要求结果为字符串且输出到屏幕上 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);       
  //post提交方式 
  curl_setopt($ch, CURLOPT_POST, 1);            
  // 增加 HTTP Header(头)里的字段  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);       
  // 终止从服务端进行验证 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
  //这个是证书的位置 
  curl_setopt($ch,CURLOPT_SSLCERT,getcwd().&#39;\cert\apiclient_cert.pem&#39;);  
  //这个也是证书的位置 
  curl_setopt($ch,CURLOPT_SSLKEY,getcwd().&#39;\cert\apiclient_key.pem&#39;);  
  //运行curl 
  $data = curl_exec($ch);                  
  //关闭curl 
  curl_close($ch); 
  return $data; 
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

php curl with csrf-token verification simulation submission example detailed explanation

PHP SPL application case detailed explanation

Detailed explanation of session sharing case under load balancing in PHP (with code)

The above is the detailed content of PHP WeChat official account randomly distributes cash 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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools