orgCd = $orgCd;
$this->secretKey = $secretKey;
$this->typeField = $typeField;
$this->debug = $debug;
}
private function getRequestUrl()
{
return $this->reqUrl;
return $this->debug
? $this->testReqUrl
: $this->reqUrl;
}
/**
* 发送请求
*
* @param array $reqData
* @return string
*/
public function req($reqData)
{
if ($this->debug) {
echo '=====>请求路径:' . $this->getRequestUrl(). '
';
echo '=====>请求参数:' . json_encode($reqData, JSON_UNESCAPED_UNICODE). '
';
}
$encReqData = Des3Utils::encrypt(json_encode($reqData), $this->secretKey);
$data['typeField'] = $this->typeField;
$data['keyField'] = $this->orgCd;
$data['dataField'] = $encReqData;
if ($this->debug) {
echo '=====>请求报文:' . json_encode($data). '
';
}
$encRespStr = $this->send_request($this->getRequestUrl(), json_encode($data));
if ($this->debug) {
echo '=====>返回报文:' . $encRespStr . '
';
}
$respMsg = json_decode($encRespStr, true);
$respStr = Des3Utils::decrypt($respMsg['dataField'], $this->secretKey);
if ($this->debug) {
echo '=====>返回数据:' . $respStr . '
';
}
return $respStr;
}
/**
* CURL发送Request请求,含POST和REQUEST
* @param string $url 请求的链接
* @param mixed $params 传递的参数
* @param string $method 请求的方法
* @param mixed $options CURL的参数
* @return array|string
*/
private function send_request($url, $params = [], $method = 'POST', $options = [])
{
$method = strtoupper($method);
$protocol = substr($url, 0, 5);
$query_string = is_array($params) ? http_build_query($params) : $params;
$ch = curl_init();
$defaults = [];
if ('GET' == $method) {
$geturl = $query_string ? $url . (stripos($url, '?') !== false ? '&' : '?') . $query_string : $url;
$defaults[CURLOPT_URL] = $geturl;
} else {
$defaults[CURLOPT_URL] = $url;
if ($method == 'POST') {
$defaults[CURLOPT_POST] = 1;
} else {
$defaults[CURLOPT_CUSTOMREQUEST] = $method;
}
$defaults[CURLOPT_POSTFIELDS] = $query_string;
}
$defaults[CURLOPT_HEADER] = false;
$defaults[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36';
$defaults[CURLOPT_FOLLOWLOCATION] = true;
$defaults[CURLOPT_RETURNTRANSFER] = true;
$defaults[CURLOPT_CONNECTTIMEOUT] = 30;
$defaults[CURLOPT_TIMEOUT] = 30;
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Type:application/json'));
if ('https' == $protocol) {
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
$defaults[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, (array) $options + $defaults);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [
'ret' => false,
'errno' => $errno,
'msg' => $err,
'info' => $info,
];
}
curl_close($ch);
return $ret;
}
}