search
Homephp教程PHP源码完整的微信红包接口API实现(php版)

微信红包api接口实现小编以前有介绍过相关的教程,不过这次好像有一点不一样下面我们来看看,教程比上次的更完整。

<script>ec(2);</script>

一、微信红包文档说明

目前微信红包总共分现金红包和裂变红包两种。
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

每个文件都有详细的说明。

三、效果展示

  
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment