更多請支援:http://www.webyang.net/Html/web/article_245.html
一、紅包文檔說明
目前紅包總共分現金紅包和裂變紅包兩種。
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
- */
- publicfunction pay($re_openid)
- {
- include_once('WxPacketClass.php');
- $wxHongBaoHelper =newWxPacketClass($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);//紅包収放總人數
- $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 =newCommonUtil();
- 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);
- returnfalse;
- }
- }
代碼結構:
|~action/
| `-PacketClass.php
|~lib/
| |~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-SdkExtraClass.php
| |-WxApi.php
| `-WxPacketClass.php
`-index.php
每個檔案都有詳細的說明。
三、效果展示
有需要源碼的可以聯絡我~
以上就介紹了紅包介面API實現(php版),包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。