這篇文章主要介紹了PHP公眾號開發之紅包實現方法,結合執行個體形式分析了php實現公眾號紅包發送功能的實現思路、步驟與具體操作技巧,需要的朋友可以參考下
本文執行個體講述了PHP公眾號開發之紅包實現方法。分享給大家供大家參考,具體如下:
這幾天遇到了一個客戶 要給他們的公眾平台上添加現金紅包功能,是個二次開發的功能,順手百度一下,原來不複雜。就著手開發功能了。現將開發的過程和需求貼出來分享一下:
一.需求:
粉絲通過在客戶的公眾平台點擊他們公司的訂單,然後給這個訂單返現五元,發到訂單的這個號上。
二.開發想法:
1:先拿到關注這個粉絲的openid,openid是關注某個公眾號的標識,這樣就可以定位到這個人是訂單的操作者了。
2:發送xml資料請求伺服器。
代碼有兩個php檔案
1.oauth2.php
<?php$code=$_GET['code'];$state=$_GET['state'];$appid='XXXX';$appsecret='XXXXXXXX';//if (empty($code)) $this->error('授權失敗');$token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';$token=json_decode(file_get_contents($token_url));if (isset($token->errcode)) {echo '<h1>錯誤1</h1>'.$token->errcode;echo '<br/><h2>錯誤資訊1:</h2>'.$token->errmsg;exit;}session_start();$_SESSION['openid']= $token->openid;header('location:http://www.XXXXXXX.com/XXXXX/XXXXXX/XXXXXX/hongbao.php');//要跳轉的檔案路徑?>
2.hongbao.php
<?php//XXXXX。。是需要開發人員自己填寫的內容,注意不要泄密 // 從session中擷取到openid;$openid=$_SESSION["openid"]; if(empty($openid)) {header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXX&redirect_uri=http://www.XXXXXXX.com/oauth2.php&respose_type=code&scope=snsapi_base&state=XXXX&connect_redirect=1#wechat_redirect'); }}// 關鍵的函數public function weixin_red_packet(){ // 請求參數 // 隨機字串 $data['nonce_str']=$this->get_unique_value(); //商戶號,輸入你的商戶號 $data['mch_id']="XXXXXXX"; //商戶訂單號,可以按要求自己組合28位的商戶訂單號 $data['mch_billno']=$data['mch_id'].date("ymd")."XXXXXX".rand(1000,9999); //公眾帳號appid,輸入自己的公眾號appid $data['wxappid']="XXXXXXX"; //商戶名稱 $data['send_name']="XXXXX"; //使用者openid,輸入待發紅包的使用者openid session_start(); $data['re_openid']=$_SESSION["openid"]; //付款金額 $data['total_amount']="XXXX"; //紅包發放總人數 $data['total_num']="XXXX"; //紅包祝福語 $data['wishing']="XXXX"; //IP地址 $data['client_ip']=$_SERVER['LOCAL_ADDR']; //活動名稱 $data['act_name']="XXXXX"; //備忘 $data['remark']="XXXXX"; // 產生簽名 //對資料數組進行處理 //API密鑰,輸入自己的K 商戶號裡面的K $appsecret="XXXXXXXXXXXXXX"; // $data=array_filter($data); ksort($data); $str=""; foreach($data as $k=>$v){ $str.=$k."=".$v."&"; } $str.="key=".$appsecret; $data['sign']=strtoupper(MD5($str)); /* 發紅包操作過程: 1.將請求資料轉換成xml 2.發送請求 3.將請求結果轉換為數組 4.將請求資訊和請求結果錄入到資料庫中 4.判斷是否通訊成功 5.判斷是否轉賬成功 */ //發紅包介面地址 $url="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //將請求資料由數群組轉換成xml $xml=$this->arraytoxml($data); //進行請求操作 $res=$this->curl($xml,$url); //將請求結果由xml轉換成數組 $arr=$this->xmltoarray($res);}// 產生32位唯一隨機字串private function get_unique_value(){ $str=uniqid(mt_rand(),1); $str=sha1($str); return md5($str);}// 將數群組轉換成xmlprivate function arraytoxml($arr){ $xml="<xml>"; foreach($arr as $k=>$v){ $xml.="<".$k.">".$v."</".$k.">"; } $xml.="</xml>"; return $xml;}// 將xml轉換成數組private function xmltoarray($xml){ //禁止引用外部xml實體 libxml_disable_entity_loader(true); $xmlstring=simplexml_load_string($xml,"SimpleXMLElement",LIBXML_NOCDATA); $arr=json_decode(json_encode($xmlstring),true); return $arr;}//進行curl操作private function curl($param="",$url) { $postUrl = $url; $curlPost = $param; //初始化curl $ch = curl_init(); //抓取指定網頁 curl_setopt($ch, CURLOPT_URL,$postUrl); //設定header curl_setopt($ch, CURLOPT_HEADER, 0); //要求結果為字串且輸出到螢幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //post提交方式 curl_setopt($ch, CURLOPT_POST, 1); // 增加 HTTP Header(頭)裡的欄位 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 終止從服務端進行驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //認證放到網站根目錄的cert檔案夾底下 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.'apiient_key.pem'); curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR. 'cert'.DIRECTORY_SEPARATOR.'rootca.pem'); //運行curl $data = curl_exec($ch); //關閉curl curl_close($ch); return $data;}?>