您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關PHP如何開發微信現金紅包功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
微信商家后臺-現金紅包開發
sdk
<?php class wxPay { //配置參數信息 const SHANGHUHAO = "1430998xxx";//商戶號 const PARTNERKEY = "leFyTOXWzUEXxfp47WOu5d9xxxxxx"; //api,商戶后臺 //核心支付函數,參數:請求地址和參數 function pay($url,$obj) { $obj['nonce_str'] = $this->create_noncestr(); //創建隨機字符串 $stringA = $this->create_qianming($obj,false); //創建簽名 $stringSignTemp = $stringA."&key=leFyTOXWzUEXxfp47WOu5xxxxxx"; //簽名后加api $sign = strtoupper(md5($stringSignTemp)); //簽名加密并大寫 $obj['sign'] = $sign; //將簽名傳入數組 $postXml = $this->arrayToXml($obj); //將參數轉為xml格式 var_dump($postXml); $responseXml = $this->curl_post_ssl($url,$postXml); //提交請求 var_dump($responseXml); return $responseXml; } //生成簽名,參數:生成簽名的參數和是否編碼 function create_qianming($arr,$urlencode) { $buff = ""; ksort($arr); //對傳進來的數組參數里面的內容按照字母順序排序,a在前面,z在最后(字典序) foreach ($arr as $k=>$v) { if(null!=$v && "null" != $v && "sign" != $k) { //簽名不要轉碼 if ($urlencode) { $v = urlencode($v); } $buff.=$k."=".$v."&"; } } if (strlen($buff)>0) { $reqPar = substr($buff,0,strlen($buff)-1); //去掉末尾符號“&” } return $reqPar; } //生成隨機字符串,默認32位 function create_noncestr($length=32) { //創建隨機字符 $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for($i=0;$i<$length;$i++) { $str.=substr($chars, mt_rand(0,strlen($chars)-1),1); } return $str; } //數組轉xml 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; } //post請求網站,需要證書 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文件 //請確保您的libcurl版本是否支持雙向認證,版本高于7.20.1 curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR. 'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem'); curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR. 'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem'); curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR. 'zhengshu'.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); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } }
證書文件:
請在微信商戶后臺-API安全下載,PHP請使用php的證書文件
微信紅包調用測試文件:
<?php require 'wxpay.class.php';<br><br>//數組參數 $money = 100; //最低1元,單位分 $sender = "晨緣房產"; $obj2 = array(); $obj2['wxappid'] = ""; //appid $obj2['mch_id'] = ""; //商戶id $obj2['mch_billno'] = "商戶id".date('YmdHis').rand(1000,9999); //組合成28位,根據官方開發文檔,可以自行設置 $obj2['client_ip'] = $_SERVER['REMOTE_ADDR']; $obj2['re_openid'] = "oUxO6v26wdyMHThD9kfDW6NB_xbE"; //接收紅包openid $obj2['total_amount'] = $money; $obj2['min_value'] = $money; $obj2['max_value'] = $money; $obj2['total_num'] = 1; $obj2['nick_name'] = $sender; $obj2['send_name'] = $sender; $obj2['wishing'] = "恭喜發財"; $obj2['act_name'] = $sender."紅包"; $obj2['remark'] = $sender."紅包"; $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; $wxpay = new wxPay(); $res = $wxpay->pay($url, $obj2); var_dump($res);
關于“PHP如何開發微信現金紅包功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。