php版微信发红包接口用法示例

网络编程 2021-07-05 09:10www.168986.cn编程入门
这篇文章主要介绍了php版微信发红包接口用法,结合实例形式分析了php操作微信发红包接口的相关使用技巧,需要的朋友可以参考下

本文实例讲述了php版微信发红包接口用法。分享给大家供大家参考,具体如下

微信红包功能我相信各位朋友都知道吧,但如果我们要把红包集成到网站那么要如何来做呢?这里长沙网络推广就来为各位分享一个php 微信发红包接口测试实例,希望文章能够帮助到各位朋友

狼蚁网站SEO优化是一个类,使用方法:

$arr['openid']='ojgTTt8oF9VdYcGsJMACHpA-jy1U';
      $arr['hbname']="提现申请";
      $arr['body']="您的提现申请已经成功";
      $arr['fee']=1;
$m = new Common_util_pub();
$re = $m->sendhongbaoto($arr);
var_dump($re);

注意证书位置和 商户后台设置的key需要修改.

<?php
header("Content-type: text/html; charset=utf-8");
class Common_util_pub
{
  /
 hbname 红包名称 fee 红包金额 /元 body 内容 openid 微信用户id
 @param undefined $arr

 @return
/
public function sendhongbaoto($arr){
//$m = new Common_util_pub();
$data['mch_id'] = '120005402';
$data['mch_billno'] = '120005402'.date("Ymd",time()).date("His",time()).rand(1111,9999);
$data['nonce_str'] = self::createNoncestr();
$data['re_openid'] = $arr['openid'];
$data['wxappid'] = 'wx8axxxxxbac4905';
$data['nick_name'] = $arr['hbname'];
$data['send_name'] = $arr['hbname'];
$data['total_amount'] = $arr['fee']100;
$data['min_value'] = $arr['fee']100;
$data['max_value'] = $arr['fee']100;
$data['total_num'] = 1;
$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
$data['act_name'] = '测试活动';
$data['remark'] = '备注一下';
$data['wishing'] = $arr['body'];
if(!$data['re_openid']) {
   $rearr['return_msg']='缺少用户openid';
   return $rearr;
}
$data['sign'] = self::getSign($data);
$xml = self::arrayToXml($data);
//var_dump($xml);
$url ="https://api.mch.weixin.qq./mmpaymkttransfers/sendredpack";
$re = self::wxHttpsRequestPem($xml,$url);
$rearr = self::xmlToArray($re);
return $rearr;
}
function trimString($value)
  {
    $ret = null;
    if (null != $value)
    {
      $ret = $value;
      if (strlen($ret) == 0)
      {
        $ret = null;
      }
    }
    return $ret;
  }
  /
    作用产生随机字符串,不长于32位
   /
  public function createNoncestr( $length = 32 )
  {
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $str ="";
    for ( $i = 0; $i < $length; $i++ ) {
      $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
    }
    return $str;
  }
  /
    作用格式化参数,签名过程需要使用
   /
  function formatBizQueryParaMap($paraMap, $urlencode)
  {
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v)
    {
      if($urlencode)
      {
        $v = urlencode($v);
      }
      //$buff .= strtolower($k) . "=" . $v . "&";
      $buff .= $k . "=" . $v . "&";
    }
    $reqPar;
    if (strlen($buff) > 0)
    {
      $reqPar = substr($buff, 0, strlen($buff)-1);
    }
    return $reqPar;
  }
  /
    作用生成签名
   /
  public function getSign($Obj)
  {
    foreach ($Obj as $k => $v)
    {
      $Parameters[$k] = $v;
    }
    //签名步骤一按字典序排序参数
    ksort($Parameters);
    $String = $this->formatBizQueryParaMap($Parameters, false);
    //echo '【string1】'.$String.'</br>';
    //签名步骤二在string后加入KEY
    $String = $String."&key="."254554sefg4exxxxxxxxs5cds1"; // 商户后台设置的key
    //echo "【string2】".$String."</br>";
    //签名步骤三MD5加密
    $String = md5($String);
    //echo "【string3】 ".$String."</br>";
    //签名步骤四所有字符转为大写
    $result_ = strtoupper($String);
    //echo "【result】 ".$result_."</br>";
    return $result_;
  }
  /
    作用array转xml
   /
  public function arrayToXml($arr)
  {
    $xml = "<xml>";
    foreach ($arr as $key=>$val)
    {
       if (is_numeric($val))
       {
        $xml.="<".$key.">".$val."</".$key.">";
       }
       else
        $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    }
    $xml.="</xml>";
    return $xml;
  }
  /
    作用将xml转为array
   /
  public function xmlToArray($xml)
  {
    //将XML转为array
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
  }
   public function wxHttpsRequestPem( $vars,$url, $second=30,$aHeader=array()){
        $ch = curl_init();
        //超时时间
        curl_set($ch,CURLOPT_TIMEOUT,$second);
        curl_set($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理,如果有的话
        //curl_set($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_set($ch,CURLOPT_PROXYPORT, 8080);
        curl_set($ch,CURLOPT_URL,$url);
        curl_set($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_set($ch,CURLOPT_SSL_VERIFYHOST,false);
        //以下两种方式需选择一种
        //第一种方法,cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_set($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_set($ch,CURLOPT_SSLCERT,dirname(__FILE__).'/hongbao/apiclient_cert.pem');
        //默认格式为PEM,可以注释
        curl_set($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_set($ch,CURLOPT_SSLKEY,dirname(__FILE__).'/hongbao/apiclient_key.pem');
        curl_set($ch,CURLOPT_CAINFO,'PEM');
        curl_set($ch,CURLOPT_CAINFO,dirname(__FILE__).'/hongbao/rootca.pem');
        //第二种方式,两个文件合成一个.pem文件
        //curl_set($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
        if( count($aHeader) >= 1 ){
            curl_set($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_set($ch,CURLOPT_POST, 1);
        curl_set($ch,CURLOPT_POSTFIELDS,$vars);
        $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;
        }
    }
}
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by