实现PHP微信红包API接口

实现PHP微信红包API接口,第1张

概述实现PHP微信红包API接口

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

相关学习推荐:php编程(视频)

根据微信高级红包接口,开发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: wxd111665abv58f4fmch_ID: 10000100device_info: 1000Body: testnonce_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()="9A0A8659F005D6984697E2CA0A9CF3B7"*/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接口全部内容,希望文章能够帮你解决实现PHP微信红包API接口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1013119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存