欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-02 14:22:52
本文实例讲述了php版微信发红包接口用法。分享给大家供大家参考,具体如下: 微信红包功能我相信各位朋友都知道吧,但如果我们要把红包集成到网站那么要如何来做呢?这里...

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

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

下面是一个类,使用方法:

$arr['openid']='ojgttt8of9vdycgsjmachpa-jy1u';
      $arr['hbname']="提现申请";
      $arr['body']="您的提现申请已经成功";
      $arr['fee']=1;
$comm = new common_util_pub();
$re = $comm->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){
//$comm = 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.com/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_setopt($ch,curlopt_timeout,$second);
        curl_setopt($ch,curlopt_returntransfer, 1);
        //这里设置代理,如果有的话
        //curl_setopt($ch,curlopt_proxy, '10.206.30.98');
        //curl_setopt($ch,curlopt_proxyport, 8080);
        curl_setopt($ch,curlopt_url,$url);
        curl_setopt($ch,curlopt_ssl_verifypeer,false);
        curl_setopt($ch,curlopt_ssl_verifyhost,false);
        //以下两种方式需选择一种
        //第一种方法,cert 与 key 分别属于两个.pem文件
        //默认格式为pem,可以注释
        curl_setopt($ch,curlopt_sslcerttype,'pem');
        curl_setopt($ch,curlopt_sslcert,dirname(__file__).'/hongbao/apiclient_cert.pem');
        //默认格式为pem,可以注释
        curl_setopt($ch,curlopt_sslkeytype,'pem');
        curl_setopt($ch,curlopt_sslkey,dirname(__file__).'/hongbao/apiclient_key.pem');
        curl_setopt($ch,curlopt_cainfo,'pem');
        curl_setopt($ch,curlopt_cainfo,dirname(__file__).'/hongbao/rootca.pem');
        //第二种方式,两个文件合成一个.pem文件
        //curl_setopt($ch,curlopt_sslcert,getcwd().'/all.pem');
        if( count($aheader) >= 1 ){
            curl_setopt($ch, curlopt_httpheader, $aheader);
        }
        curl_setopt($ch,curlopt_post, 1);
        curl_setopt($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微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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