thinkphp5 微信红包发放

<?php
namespace app\hongbao\controller;
use think\Controller;
  define('APPID','******************');
  define('MCHID','**************');
  define('SECRECT_KEY','*************');
  define('IP','127.0.0.1');
class Hongbao extends Common
{

      /**
   * [xmltoarray xml格式转换为数组]
   * @param  [type] $xml [xml]
   * @return [type]      [xml 转化为array]
   */
 public  function xmltoarray($xml) { 
       //禁止引用外部xml实体 
      libxml_disable_entity_loader(true); 
      $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
      $val = json_decode(json_encode($xmlstring),true); 
      return $val;
  }

  /**
   * [arraytoxml 将数组转换成xml格式(简单方法):]
   * @param  [type] $data [数组]
   * @return [type]       [array 转 xml]
   */
 public  function arraytoxml($data){
      $str='<xml>';
      foreach($data as $k=>$v) {
          $str.='<'.$k.'>'.$v.'</'.$k.'>';
      }
      $str.='</xml>';
      return $str;
  }

  /**
   * [createNoncestr 生成随机字符串]
   * @param  integer $length [长度]
   * @return [type]          [字母大小写加数字]
   */
 public  function createNoncestr($length =32){
      $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";  
      $str ="";

      for($i=0;$i<$length;$i++){  
          $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);   
      }  
      return $str;
  }
 public  function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
      $isdir ="/www/wwwroot/cloud.veiding.com/vendor/wxpay/cacert/";//证书位置;绝对路径

      $ch = curl_init();//初始化curl

      curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
      curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
      curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
      curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
      curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
      curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
      curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
     // curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
      if (count($aHeader) >= 1) {
          curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
      }
      curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
      curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送

      $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;
      }
  }
   protected function sendMoney($amount,$openid){
      
      $total_amount = (100) * $amount;
      
      $data=array(
          'wxappid'=>APPID,//商户账号appid
          'mch_id'=> MCHID,//商户号
          'send_name'=> 'ceshi',//发送者名称
          'nonce_str'=>$this->createNoncestr(),//随机字符串
          'mch_billno'=> date('YmdHis').rand(1000, 9999),//商户订单号
          'act_name'=>'ceshi',
          're_openid'=> $openid,//用户openid
          'total_num'=>1,
          'total_amount'=> $total_amount,//金额
          'wishing'=>'恭喜您中奖了',//企业付款描述信息
          'remark'=>'幸运奖品',
          'scene_id'=>'PRODUCT_2',
          'client_ip'=> IP,//Ip地址
      );

      //生成签名算法
      $secrect_key=SECRECT_KEY;///这个就是个API密码。MD5 32位。
      $data=array_filter($data);
      ksort($data);
      $str='';
      foreach($data as $k=>$v) {
         $str.=$k.'='.$v.'&';
      }
      $str.='key='.$secrect_key;
      $sign=md5($str);
      $data['sign']=strtoupper($sign);
   
      //生成签名算法

  
      $xml=$this->arraytoxml($data);
  
      $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; //调用接口
      $res=$this->curl_post_ssl($url,$xml);
     
      $return=$this->xmltoarray($res);
      
     $return['trade_no']=$data['mch_billno'];
      return $return;
    // var_dump($return);
    //   $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
    //   $res= $responseObj->return_code;  //SUCCESS  如果返回来SUCCESS,则发生成功,处理自己的逻辑
    //  var_dump($res);
     //  return $res;
  }
  
  //发放红包
   public  function  getmoney(){
       
   
      $res= $this->sendMoney('1','openid');
      var_dump($res);
   

   
   }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值