对于微信红包我相信今年过年时各位朋友都有知道吧,这个微信红包相当的火爆了,下面我们来给各位介绍一个PHP版微信红包API接口程序了,我们可以基于这个接口也实现开发第三方红包功能.
根据微信高级红包接口,开发PHP版本的API接口,现在进行主要代码分析,红包接口调用请求代码,所有请求参数为必填参数与文档对应:
-
class Wxapi {
-
private $app_id = 'wxXXXXXXXXXXXX';
-
private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';
-
private $app_mchid = 'XXXXXXXX';
-
function __construct(){
-
-
}
-
-
-
-
-
public function pay($re_openid)
-
{
-
include_once('WxHongBaoHelper.php');
-
$commonUtil = new CommonUtil();
-
$wxHongBaoHelper = new WxHongBaoHelper();
-
$wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());
-
$wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));
-
$wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);
-
$wxHongBaoHelper->setParameter("wxappid", $this->app_id);
-
$wxHongBaoHelper->setParameter("nick_name", '红包');
-
$wxHongBaoHelper->setParameter("send_name", '红包');
-
$wxHongBaoHelper->setParameter("re_openid", $re_openid);
-
$wxHongBaoHelper->setParameter("total_amount", 100);
-
$wxHongBaoHelper->setParameter("min_value", 100);
-
$wxHongBaoHelper->setParameter("max_value", 100);
-
$wxHongBaoHelper->setParameter("total_num", 1);
-
$wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');
-
$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;
-
}
-
获取随机字符串方法:
-
-
-
-
-
public function great_rand(){
-
$str = '1234567890abcdefghijklmnopqrstuvwxyz';
-
for($i=0;$i<30;$i++){
-
$j=rand(0,35);
-
$t1 .= $str[$j];
-
}
-
return $t1;
-
}
签名算法:
-
/**
-
例如:
-
appid: wxd111665abv58f4f
-
mch_id: 10000100
-
device_info: 1000
-
Body: test
-
nonce_str: ibuaiVcKdpRxkhJA
第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";
第二步:拼接支付密钥:
-
stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
-
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
-
9CF3B7"
-
*/
-
protected function get_sign(){
-
define('PARTNERKEY',"QSRXXXXXXXXXXXXXXXXXXXXX");
-
try {
-
if (null == PARTNERKEY || "" == PARTNERKEY ) {
-
throw new SDKRuntimeException("密钥不能为空!" . "<br>");
-
}
-
if($this->check_sign_parameters() == false) {
-
throw new SDKRuntimeException("生成签名参数缺失!" . "<br>");
-
}
-
$commonUtil = new CommonUtil();
-
ksort($this->parameters);
-
$unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
-
$md5SignUtil = new MD5SignUtil();
-
return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
-
}catch (SDKRuntimeException $e)
-
{
-
die($e->errorMessage());
-
}
-
-
}
CURL请求以及发送证书:
-
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);
-
-
-
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);
-
-
curl_close($ch);
-
return false;
-
}
-
}
入口文件:
-
@require "pay.php";
-
-
$get = $_GET['param'];
-
$code = $_GET['code'];
-
-
if($get=='access_token' && !emptyempty($code)){
-
$param['param'] = 'access_token';
-
$param['code'] = $code;
-
$packet = new Packet();
-
-
$userinfo = $packet->_route('userinfo',$param);
-
if(emptyempty($userinfo['openid'])){
-
exit("NOAUTH");
-
}
-
-
$packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
-
}else{
-
$packet->_route('userinfo');
-
}