/** * 以post方式提交xml到对应的接口url * * @param string $xml 需要post的xml数据 * @param string $url url * @param bool $useCert 是否需要证书,默认不需要 * @param int $second url执行超时时间,默认30s * @throws WxPayException */ public function executeXmlCurl(WxPayDataBase $payDataBase, $url, $method = self::METHOD_POST, $useCert = false, $second = 30) { $xml = $payDataBase->ToXml(); $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); //如果有配置代理这里就设置代理 if (WxPayConfig::CURL_PROXY_HOST != "0.0.0.0" && WxPayConfig::CURL_PROXY_PORT != 0) { curl_setopt($ch, CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST); curl_setopt($ch, CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //严格校验 //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if ($useCert == true) { //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLCERT, WxPayConfig::getSslCertPath()); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, WxPayConfig::getSslKeyPath()); } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); throw new WxPayException("curl出错,错误码:{$error}"); } }
/** * 生成签名 * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值 */ public function MakeSign() { //签名步骤一:按字典序排序参数 ksort($this->values); $string = $this->ToUrlParams(); //签名步骤二:在string后加入KEY $string = $string . "&key=" . WxPayConfig::getKey(); //签名步骤三:MD5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $result = strtoupper($string); return $result; }
/** * * 测速上报,该方法内部封装在report中,使用时请注意异常流程 * WxPayReport中interface_url、return_code、result_code、user_ip、execute_time_必填 * appid、mchid、spbill_create_ip、nonce_str不需要填入 * @param WxPayReport $inputObj * @param int $timeOut * @throws WxPayException * @return 成功时返回,其他抛异常 */ private static function report($inputObj, $timeOut = 1) { $url = "https://api.mch.weixin.qq.com/payitil/report"; //检测必填参数 if (!$inputObj->IsInterface_urlSet()) { throw new WechatPayException("接口URL,缺少必填参数interface_url!"); } if (!$inputObj->IsReturn_codeSet()) { throw new WechatPayException("返回状态码,缺少必填参数return_code!"); } if (!$inputObj->IsResult_codeSet()) { throw new WechatPayException("业务结果,缺少必填参数result_code!"); } if (!$inputObj->IsUser_ipSet()) { throw new WechatPayException("访问接口IP,缺少必填参数user_ip!"); } if (!$inputObj->IsExecute_time_Set()) { throw new WechatPayException("接口耗时,缺少必填参数execute_time_!"); } $inputObj->SetAppid(WxPayConfig::getAppId()); //公众账号ID $inputObj->SetMch_id(WxPayConfig::getMchID()); //商户号 $inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']); //终端ip $inputObj->SetTime(date("YmdHis")); //商户上报时间 $inputObj->SetNonce_str(self::getNonceStr()); //随机字符串 $inputObj->SetSign(); //签名 $startTimeStamp = self::getMillisecond(); //请求开始时间 $response = self::getHttpClient()->executeXmlCurl($inputObj, $url, self::METHOD_POST, false, $timeOut); return $response; }