发放相关配置_cert[$file] = $cret_file; } } catch (exception $e) { throw new exception('证书错误'); } } } /** * 红包发放 * @param array $parameters 发放所需参数 * @return boolean */ public function sendredpack($parameters) { // 官方硬性要求 $stime = date('h:i'); if ($stime error = '请北京时间0:00-8:00时间之外触发红包赠送'; return false; } $bonus = array(); $this->setparameter('mch_id', $this->_keys['mch_id']); $this->setparameter('wxappid', $this->_keys['wxappid']); $must = array( 're_openid', 'nick_name', 'send_name', 'total_amount', 'wishing', 'act_name', 'remark', 'client_ip' ); foreach ($must as $key) { if (isset($parameters[$key]) && $parameters[$key]) { $this->setparameter($key, $parameters[$key]); } else if (! isset($this->_parameters[$key]) || ! $this->_parameters[$key]) { $this->error = '参数缺损:' . $key; return false; } } if (! isset($parameters['mch_billno'])) { $parameters['mch_billno'] = $this->getmchbillno(); } $this->setparameter('mch_billno', $parameters['mch_billno']); $this->setparameter('total_num', 1); $this->setparameter('nonce_str', $this->getrand(30, 3)); $postxml = $this->_createxml(); if (! $postxml) { return false; } $this->log($postxml, 'send_xml'); $result = $this->curl_post_ssl(self::sendredpack_url, $postxml); $this->log($result, 'result_xml'); if (! $result) { return false; } $resultobj = simplexml_load_string($result, 'simplexmlelement', libxml_nocdata); $this->_lastresult = $resultobj; if ($resultobj->return_code == 'success') { // 成功标识 if ($resultobj->result_code == 'success') { return $resultobj->send_listid; } if ($resultobj->return_msg) { $this->error = (string) $resultobj->return_msg; return false; } $this->error = (string) $resultobj->err_code_des; return false; } if ($resultobj->return_code != 'fail') { $this->error = '返回信息格式异常'; return false; } $this->error = (string) $resultobj->return_msg; return false; } /** * 查询红包的发放详情 * 参数: * mch_billno 发送红包时生产的唯一订单号 * 返回: * 成功:object对象 * 失败:false **/ public function getinfo($mch_billno){ $param = array( 'nonce_str' => $this->getrand(30, 3), 'mch_billno'=> $mch_billno , 'mch_id' => $this->_keys['mch_id'], 'appid' => $this->_keys['wxappid'], 'bill_type' => 'mcht', ); ksort($param); $unsignparastring = $this->_formatqueryparamap($param, false); $param['sign'] = $this->_sign($unsignparastring, $this->_keys['key']); $xml = $this->arraytoxml($param); $this->log($xml, 'getinfo_xml'); $result = $this->curl_post_ssl(wepay::gethbinfo_url, $xml); if(!$result){ return false ; } $this->log($result, 'result_xml'); $resultobj = simplexml_load_string($result, 'simplexmlelement', libxml_nocdata); $this->_lastresult = $resultobj ; if($resultobj->return_code == 'success'){//成功标识 if($resultobj->result_code == 'success'){ return $resultobj ; } if($resultobj->return_msg){ $this->error = $resultobj->return_msg ; return false ; } $this->error = $resultobj->err_code_des ; return false ; } if($resultobj->return_code != 'fail'){ $this->error = '返回信息格式异常'; return false ; } $this->error = $resultobj->return_msg ; return false ; } /** * 设置红包发放所需要的参数 * @param $parameter 键值数组/键 * @param $value 值 * @return wxbonusapi */ public function setparameter($parameter, $value = null) { if (! is_array($parameter)) { return $this->setparameter(array( $parameter => $value )); } foreach ($parameter as $key => $value) { $key = trim($key); $value = trim($value); $this->_parameters[$key] = $value; } return $this; } /** * 获取参数值 * @param $parameter 键名 * @return multitype: */ public function getparameter($parameter) { return $this->_parameters[$parameter]; } /** * 获取随机数 * @param number $len 随机数的位数 * @param number $type 取值范围 1表示数字 2小写字母 4大写字母 * @return string */ public function getrand($len = 30, $type = 0) { $str = ''; $max = - 1; if (! $type) { $type = 3; } if ($type & 1) { $str .= '1234567890'; $max += 10; } if ($type & 2) { $str .= 'abcdefghijklmnopqrstuvwxyz'; $max += 26; } if ($type & 4) { $str .= 'abcdefghijklmnopqrstuvwxyz'; $max += 26; } $rand = ''; for ($i = 0; $i _lastmchbillno = $this->_parameters['mch_id'] . date('ymdhis') . $this->getrand(4, 1); // $this->getrandnum(); return $this->_lastmchbillno; } /** * 获取最后一次创建红包时生成的订单号 * @return string */ public function getlastmchbillno() { return $this->_lastmchbillno; } /** * 创建xml的方法 * @param number $retcode * @param string $reterrmsg * @return boolean|string */ private function _createxml() { try { $sign = $this->_getsign(); if (! $sign) { return false; } $this->setparameter('sign', $sign); return $this->arraytoxml($this->_parameters); } catch (exception $e) { $this->error = $e->getmessage(); return false; } } /** * 参数转换成xml * @param array $arr 参数数组 * @return string */ public function arraytoxml($arr) { $xml = ; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= . $val . . $key . >; } else { $xml .= . $key . >; } } $xml .= ; return $xml; } /** * 获得签名结果 * @return boolean|ambigous */ protected function _getsign() { try { if ($this->_checksign() == false) { // 检查生成签名参数 $this->error = '生成签名参数缺失!'; $this->log(json_encode($this->_parameters, json_unescaped_unicode), 'error_sign_xml'); return false; } ksort($this->_parameters); $unsignparastring = $this->_formatqueryparamap($this->_parameters, false); return $this->_sign($unsignparastring, $this->_keys['key']); } catch (exception $e) { $this->error = $e->getmessage(); return false; } } /** * 检查签名所需参数是否齐全 * @return boolean */ private function _checksign() { // return true; if ($this->_parameters[nonce_str] == null || $this->_parameters[mch_billno] == null || $this->_parameters[mch_id] == null || $this->_parameters[wxappid] == null || $this->_parameters[nick_name] == null || $this->_parameters[send_name] == null || $this->_parameters[re_openid] == null || $this->_parameters[total_amount] == null || $this->_parameters[total_num] == null || $this->_parameters[wishing] == null || $this->_parameters[client_ip] == null || $this->_parameters[act_name] == null || $this->_parameters[remark] == null) { return false; } return true; } /** * * @param $paramap * @param $urlencode * @return string */ private function _formatqueryparamap($paramap,$urlencode) { $buff = ; ksort($paramap); foreach ($paramap as $k => $v) { if (null != $v && null != $v && sign != $k) { if ($urlencode) { $v = urlencode($v); } $buff .= $k . = . $v . &; } } $reqpar; if (strlen($buff) > 0) { $reqpar = substr($buff, 0, strlen($buff) - 1); } return $reqpar; } /** * 签名 * @param $content 签名的字符串 * @param $key 密钥 * @throws exception * @return string|boolean */ private function _sign($content, $key) { try { if (null == $key) { $this->error = '签名key不能为空!'; return false; } if (null == $content) { $this->error = '签名内容不能为空'; return false; } $signstr = $content . &key= . $key; return strtoupper(md5($signstr)); } catch (exception $e) { $this->error = $e->getmessage(); return false; } } /** * curl抓取 * * @param $url 链接地址 * @param $vars 参数 * @param * $second * @param * $aheader * @return mixed|boolean */ function curl_post_ssl($url, $data, $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文件 curl_setopt($ch, curlopt_sslcert, $this->_cert['api_cert']); curl_setopt($ch, curlopt_sslkey, $this->_cert['api_key']); curl_setopt($ch, curlopt_cainfo, $this->_cert['rootca']); if (count($aheader) >= 1) { curl_setopt($ch, curlopt_httpheader, $aheader); } curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $data); $data = curl_exec($ch); if ($data) { curl_close($ch); return $data; } else { $this->log(json_encode($this->_cert)); $this->error = 'aa:'.curl_errno($ch); curl_close($ch); return false; } } /** * 获取服务器ip * * @return string */ public function getserverip() { $server_ip = '127.0.0.1'; if (isset($_server)) { if (isset($_server['server_addr']) && $_server['server_addr']) { $server_ip = $_server['server_addr']; } elseif (isset($_server['local_addr']) && $_server['local_addr']) { $server_ip = $_server['local_addr']; } } else { $server_ip = getenv('server_addr'); } return $server_ip; } /** * 设置日志目录文件 * * @param unknown $file */ public function setlogfile($file) { $this->log_file = $file; } /** * 写日志 * * @param $msg 写入的信息 * @param $type 日志类型作为查询标示 */ public function log($msg, $type) { if ($this->log_file) { $log = str_replace(array( \r\n, \r, \n ), array( , , ), $msg); error_log($type . ' ' . date('y-m-d h:i:s') . ' ' . $log . \r\n, 3, $this->log_file); } }}
测试用发放demowxbonusconfig::appid, 'mch_id'=>wxbonusconfig::mchid, 'key'=>wxbonusconfig::key, 'api_cert'=>wxbonusconfig::getrealpath(). wxbonusconfig::sslcert_path,//三个路径都是绝对路径 'api_key'=>wxbonusconfig::getrealpath().wxbonusconfig::sslkey_path, 'rootca'=>wxbonusconfig::getrealpath().wxbonusconfig::sslrootca ) ); $param=array( 're_openid'=>'openid',//发送的openid 'nick_name'=>'测试',//没有用,但是需要的参数 'send_name'=>'test',//收到的红包名称 'total_amount'=>'100',//金额 分 'wishing'=>'祝福语',//红包祝福语,不超过32个字符 'act_name'=>'活动名称',//活动名称,不会显示 'remark'=>'备注',//备注,不会显示 'client_ip'=>$wxbonus->getserverip()//发送的服务器ip地址 ); $wxbonus->setlogfile('d:\\log1.txt');//设置日志文件 $result=$wxbonus->sendredpack($param);//发送红包 //结果就是true / false 如果是false 错误信息就在 error 中 if($result){ echo ok; return true; } echo $wxbonus->error; return false; } }
最后附上代码下载地址:
链接:http://pan.baidu.com/s/1o7p6gxg 密码:shjz
以上就介绍了微信红包发放类封装,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。