您好,欢迎访问一九零五行业门户网

完整的微信红包接口API实现(php版)

微信红包api接口实现小编以前有介绍过相关的教程,不过这次好像有一点不一样下面我们来看看,教程比上次的更完整。
一、微信红包文档说明
目前微信红包总共分现金红包和裂变红包两种。
1、现金红包:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
2、裂变红包:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=16_5
更多请查看:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php
二、php接口实现
本文讲解现金红包的调用,其他的大体一样,就不去尝试了。
参数说明:
代码实现:
片段一、
/**
 * 微信支付
 * @param string $openid 用户openid
 */
public function pay($re_openid)
{
    include_once('wxpacketclass.php');
    $wxhongbaohelper = new wxpacketclass($this->app_sign);
    $wxhongbaohelper->setparameter(nonce_str, $this->great_rand());//随机字符串,丌长于 32 位
    $wxhongbaohelper->setparameter(mch_billno, $this->app_mchid.date('ymdhis').rand(1000, 9999));//订单号(28位)
    $wxhongbaohelper->setparameter(mch_id, $this->app_mchid);//商户号
    $wxhongbaohelper->setparameter(wxappid, $this->app_id);
    $wxhongbaohelper->setparameter(send_name, '扬和宏科技');//红包发送者名称
    $wxhongbaohelper->setparameter(re_openid, $re_openid);//openid
    $wxhongbaohelper->setparameter(total_amount, 100);//付款金额,单位分
    $wxhongbaohelper->setparameter(total_num, 1);//红包?х抛苋耸?br />    $wxhongbaohelper->setparameter(wishing, '给您拜个晚年,祝您晚年幸福!');//红包祝福诧
    $wxhongbaohelper->setparameter(client_ip, '127.0.0.1');//调用接口的机器 ip 地址
    $wxhongbaohelper->setparameter(act_name, '拜年红包活动');//活劢名称
    $wxhongbaohelper->setparameter(remark, '大家快来抢!');//备注信息
    $postxml = $wxhongbaohelper->create_hongbao_xml();
    $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
    $responsexml = $wxhongbaohelper->curl_post_ssl($url, $postxml);
    $responseobj = simplexml_load_string($responsexml, 'simplexmlelement', libxml_nocdata);
    return $responseobj->return_code;
}
片段二、
//生成红包接口xml信息
/*
![cdata[e1ee61a9]]
 ![cdata[00100]]
 ![cdata[888]]
 ![cdata[wxcbda96de0b165486]]
 ![cdata[send_name]]
 ![cdata[onqojjxxxxxxxxx]]
 ![cdata[100]]
 ![cdata[1]]
 ![cdata[恭喜发财]]
 ![cdata[127.0.0.1]]
 ![cdata[新年红包]]
 ![cdata[act_id]]
 ![cdata[新年红包]]
*/
function create_hongbao_xml($retcode = 0, $reterrmsg = ok){
  try {
     $this->setparameter('sign', $this->get_sign());
     $commonutil = new commonutil();
     return  $commonutil->arraytoxml($this->parameters);
 }catch (sdkexception $e) {
  die($e->errormessage());
 }  
}
片段三、
function curl_post_ssl($url, $vars, $second=30,$aheader=array()) {
 $ch = curl_init();
 //超时时间
 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);
//cert 与 key 分别属于两个.pem文件
 curl_setopt($ch,curlopt_sslcert,dirname(__file__).directory_separator.'cert'.directory_separator.'apiclient_cert.pem');
 curl_setopt($ch,curlopt_sslkey,dirname(__file__).directory_separator.'cert'.directory_separator.'apiclient_key.pem');
 curl_setopt($ch,curlopt_cainfo,dirname(__file__).directory_separator.'cert'.directory_separator.'rootca.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);
  curl_close($ch);
  return false;
 }
}
代码结构:
|~action/
| `-packetclass.php
|~lib/
| |~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-sdkextraclass.php
| |-wxapi.php
| `-wxpacketclass.php
`-index.php
每个文件都有详细的说明。
三、效果展示
其它类似信息

推荐信息