這幾天遇到了一個客戶 要給他們的微信公衆平台上添加微信現金紅包功能,是個二次開發的功能,順手百度一下,原來不複雜。就着手開發功能了。現将開發的過程和需求貼出來分享一下:
一.需求:粉絲通過在客戶的公衆平台點選他們公司的訂單,然後給這個訂單返現五元,發到訂單的這個微信号上。
二.開發想法: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));
//發紅包接口位址
$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);
}
// 将數組轉換成xml
private 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;
}
?>
有問題随時問我 qq:353388292
歡迎關注我的微信公共平台:一起學習thinkPHP