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

一、微信红包文档说明目前微信红包总共分现金红包和裂变红包两种。
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接口实现
本文讲解现金红包的调用,其他的大体一样,就不去尝试了。
参数说明:

代码实现:
片段一、

  1. /**
  2. * 微信支付
  3. * @param string $openid 用户openid
  4. */
  5. publicfunction pay($re_openid)
  6. {
  7. include_once('WxPacketClass.php');
  8. $wxHongBaoHelper =newWxPacketClass($this->app_sign);
  9. $wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//随机字符串,丌长于 32 位
  10. $wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000,9999));//订单号(28位)
  11. $wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商户号
  12. $wxHongBaoHelper->setParameter("wxappid", $this->app_id);
  13. $wxHongBaoHelper->setParameter("send_name",'扬和宏科技');//红包发送者名称
  14. $wxHongBaoHelper->setParameter("re_openid", $re_openid);//openid
  15. $wxHongBaoHelper->setParameter("total_amount",100);//付款金额,单位分
  16. $wxHongBaoHelper->setParameter("total_num",1);//红包収放总人数
  17. $wxHongBaoHelper->setParameter("wishing",'给您拜个晚年,祝您晚年幸福!');//红包祝福诧
  18. $wxHongBaoHelper->setParameter("client_ip",'127.0.0.1');//调用接口的机器 Ip 地址
  19. $wxHongBaoHelper->setParameter("act_name",'拜年红包活动');//活劢名称
  20. $wxHongBaoHelper->setParameter("remark",'大家快来抢!');//备注信息
  21. $postXml = $wxHongBaoHelper->create_hongbao_xml();
  22. $url ='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
  23. $responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
  24. $responseObj = simplexml_load_string($responseXml,'SimpleXMLElement', LIBXML_NOCDATA);
  25. return $responseObj->return_code;
  26. }

片段二、

  1. //生成红包接口XML信息
  2. /*
  3. <xml>
  4. <sign>![CDATA[E1EE61A9]]</sign>
  5. <mch_billno>![CDATA[00100]]</mch_billno>
  6. <mch_id>![CDATA[888]]</mch_id>
  7. <wxappid>![CDATA[wxcbda96de0b165486]]</wxappid>
  8. <send_name>![CDATA[send_name]]</send_name>
  9. <re_openid>![CDATA[onqOjjXXXXXXXXX]]</re_openid>
  10. <total_amount>![CDATA[100]]</total_amount>
  11. <total_num>![CDATA[1]]</total_num>
  12. <wishing>![CDATA[恭喜发财]]</wishing>
  13. <client_ip>![CDATA[127.0.0.1]]</client_ip>
  14. <act_name>![CDATA[新年红包]]</act_name>
  15. <act_id>![CDATA[act_id]]</act_id>
  16. <remark>![CDATA[新年红包]]</remark>
  17. </xml>
  18. */
  19. function create_hongbao_xml($retcode =0, $reterrmsg ="ok"){
  20. try{
  21. $this->setParameter('sign', $this->get_sign());
  22. $commonUtil =newCommonUtil();
  23. return $commonUtil->arrayToXml($this->parameters);
  24. }catch(SDKException $e){
  25. die($e->errorMessage());
  26. }
  27. }

片段三、

  1. function curl_post_ssl($url, $vars, $second=30,$aHeader=array()){
  2. $ch = curl_init();
  3. //超时时间
  4. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  5. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  6. //这里设置代理,如果有的话
  7. curl_setopt($ch,CURLOPT_URL,$url);
  8. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  9. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  10. //cert 与 key 分别属于两个.pem文件
  11. curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
  12. curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
  13. curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'rootca.pem');
  14. if( count($aHeader)>=1) curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
  15. curl_setopt($ch,CURLOPT_POST,1);
  16. curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
  17. $data = curl_exec($ch);
  18. if($data){
  19. curl_close($ch);
  20. return $data;
  21. }else{
  22. $error = curl_errno($ch);
  23. curl_close($ch);
  24. returnfalse;
  25. }
  26. }

代码结构:
|~action/
| `-PacketClass.php
|~lib/
| |~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-SdkExtraClass.php
| |-WxApi.php
| `-WxPacketClass.php
`-index.php
每个文件都有详细的说明。

三、效果展示

  

有需要源码的可以联系我(www.webyang.net给我留言即可)~

猜你喜欢

转载自ycdyx.iteye.com/blog/2280832