Home >Backend Development >PHP Tutorial >php WeChat public account development cash red envelope

php WeChat public account development cash red envelope

不言
不言Original
2018-04-17 11:14:041956browse

This article mainly introduces the cash red envelope development of php WeChat public account in detail. It has certain reference value. Interested friends can refer to it.

The example of this article is to share php WeChat with everyone. The specific code of the public account’s cash red envelope is for your reference. The specific content is as follows

The following reference to the development of cash red envelope in WeChat payment is from 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; 
}

Related recommendations:

PHP WeChat API access and keyword automatic reply

Example of WeChat refund function developed by PHP WeChat payment

The above is the detailed content of php WeChat public account development cash red envelope. 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