欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP微信红包API接口

程序员文章站 2022-10-06 23:22:34
首先给大家看一看这个表格: 根据微信高级红包接口,开发php版本的api接口,现在进行主要代码分析。 红包接口调用请求代码,所有请求参数为必填参数与文档对应:...

首先给大家看一看这个表格:

PHP微信红包API接口

根据微信高级红包接口,开发php版本的api接口,现在进行主要代码分析。

红包接口调用请求代码,所有请求参数为必填参数与文档对应:

class wxapi {
 private $app_id = 'wxxxxxxxxxxxxx'; //公众账号appid,首先申请与之配套的公众账号
 private $app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxx';//公众号secret,用户获取用户授权token
 private $app_mchid = 'xxxxxxxx';//商户号id
 function __construct(){
 //do sth here....
 }
 /**
  * 微信支付
  * @param string $openid 用户openid
  */
 public function pay($re_openid)
 {
  include_once('wxhongbaohelper.php');
  $commonutil = new commonutil();
  $wxhongbaohelper = new wxhongbaohelper();
  $wxhongbaohelper->setparameter("nonce_str", $this->great_rand());//随机字符串,丌长于 32 位
  $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);//相对于医脉互通的openid
  $wxhongbaohelper->setparameter("total_amount", 100);//付款金额,单位分
  $wxhongbaohelper->setparameter("min_value", 100);//最小红包金额,单位分
  $wxhongbaohelper->setparameter("max_value", 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);
  //用作结果调试输出
  //echo htmlentities($responsexml,ent_compat,'utf-8');
 $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_i
d=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);  
 //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;
 }
}

入口文件:

@require "pay.php";
//获取用户信息
$get = $_get['param'];
$code = $_get['code'];
//判断code是否存在
if($get=='access_token' && !empty($code)){
 $param['param'] = 'access_token';
 $param['code'] = $code;
 $packet = new packet();
 //获取用户openid信息
 $userinfo = $packet->_route('userinfo',$param);
 if(empty($userinfo['openid'])){
  exit("noauth");
 }
 //调取支付方法
 $packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
}else{
 $packet->_route('userinfo');
}

以上就是关于php微信红包api接口的详细代码,分享给大家,希望对大家的学习有所帮助。