PHP微信红包生成代码分享(5)

/** * 生成随机数 */ 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'); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e62d370074f91a95783baecc4d9c9052.html