url = $url; $this->appId = $appId; $this->appSecret = $appSecret; } /** * @param $ctuRequest */ public function checkRisk($ctuRequest, $timeout) { // 计算签名 $sign = SignUtil::sign($this->appSecret, $ctuRequest); // 拼接请求URL $requestUrl = $this->url . "?appKey=" . $this->appId . "&sign=" . $sign . "&version=" . CtuClient::VERSION; $reqJsonString = json_encode($ctuRequest, JSON_UNESCAPED_UNICODE); $data = base64_encode($reqJsonString); return $this->do_post_request($requestUrl, $data, $timeout); } public function do_post_request($url, $data, $timeout = 2) { $ctuResponse = new CtuResponse(""); $params = array('http' => array( 'method' => 'POST', 'content' => $data, 'header' => 'Content-type:text/html', 'timeout' => $timeout )); $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { $ctuResponse->result = array('riskLevel' => 'ACCEPT', 'msg' => 'server connect failed!'); $this->close($fp); return json_encode($ctuResponse, JSON_FORCE_OBJECT); } $response = @stream_get_contents($fp); if ($response === false) { $ctuResponse->result = array('riskLevel' => 'ACCEPT', 'msg' => 'get response failed!'); $this->close($fp); return json_encode($ctuResponse, JSON_FORCE_OBJECT); } $this->close($fp); return $response; } public function close($fp) { try { if ($fp != null) { fclose($fp); } } catch (Exception $e) { echo "close error:" . $e->getMessage(); } } }