|
2 | 2 | namespace Jd; |
3 | 3 |
|
4 | 4 | use \Exception; |
| 5 | +use Yii; |
| 6 | +use yii\helpers\FileHelper; |
| 7 | +use yii\helpers\Json; |
5 | 8 |
|
6 | 9 | class JdClient |
7 | 10 | { |
@@ -63,20 +66,21 @@ public function curl($url, $postFields = null) |
63 | 66 | if ($postMultipart) { |
64 | 67 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
65 | 68 | } else { |
66 | | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
| 69 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1)); |
67 | 70 | } |
68 | 71 | } |
69 | | - $reponse = curl_exec($ch); |
| 72 | + $response = curl_exec($ch); |
| 73 | + $this->recordAccessOutLog($ch, 'POST', $url, $response, $postFields, ['logResponse' => true]); |
70 | 74 | if (curl_errno($ch)) { |
71 | | - throw new Exception(curl_error($ch),0); |
| 75 | + throw new Exception(curl_error($ch), 0); |
72 | 76 | } else { |
73 | 77 | $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
74 | 78 | if (200 !== $httpStatusCode) { |
75 | | - throw new Exception($reponse,$httpStatusCode); |
| 79 | + throw new Exception($response, $httpStatusCode); |
76 | 80 | } |
77 | 81 | } |
78 | 82 | curl_close($ch); |
79 | | - return $reponse; |
| 83 | + return $response; |
80 | 84 | } |
81 | 85 |
|
82 | 86 | public function execute($request, $access_token = null) |
@@ -133,7 +137,6 @@ public function execute($request, $access_token = null) |
133 | 137 |
|
134 | 138 | //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
135 | 139 | if (false === $respWellFormed) { |
136 | | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
137 | 140 | $result->code = 0; |
138 | 141 | $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
139 | 142 | return $result; |
@@ -169,4 +172,65 @@ public function exec($paramsArray) |
169 | 172 | } |
170 | 173 | return $this->execute($req, $session); |
171 | 174 | } |
| 175 | + |
| 176 | + private function recordAccessOutLog($ch, $method, $url, $content, $params, $options = []) |
| 177 | + { |
| 178 | + // 如果是跑test case的直接跳过 |
| 179 | + if (YII_ENV == 'test') { |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + $info = curl_getinfo($ch); |
| 184 | + $message = [ |
| 185 | + 'type' => 'accessOut', |
| 186 | + 'remoteAddr' => $info['primary_ip'], |
| 187 | + 'reqId' => defined('REQUEST_ID') ? REQUEST_ID : '00000000-0000-0000-0000-000000000000', |
| 188 | + 'scheme' => parse_url($url, PHP_URL_SCHEME), |
| 189 | + 'host' => parse_url($url, PHP_URL_HOST), |
| 190 | + 'port' => $info['primary_port'], |
| 191 | + 'method' => $method, |
| 192 | + 'url' => substr($url, strpos($url, parse_url($url, PHP_URL_PATH))), |
| 193 | + 'responseStatus' => $info['http_code'], |
| 194 | + 'responseTime' => floor($info['total_time'] * 1000), // 单位:毫秒 |
| 195 | + 'others' => [ |
| 196 | + 'contentType' => $info['content_type'], |
| 197 | + 'namelookupTime' => floor($info['namelookup_time'] * 1000), |
| 198 | + 'connectTime' => floor($info['connect_time'] * 1000), |
| 199 | + 'pretransferTime' => floor($info['pretransfer_time'] * 1000), |
| 200 | + 'starttransferTime' => floor($info['starttransfer_time'] * 1000), |
| 201 | + ], |
| 202 | + ]; |
| 203 | + |
| 204 | + if (!empty($params) && in_array($method, ['POST', 'PUT'])) { |
| 205 | + $message['body'] = $params; |
| 206 | + } |
| 207 | + |
| 208 | + if (isset($options['logResponse']) && $options['logResponse']) { |
| 209 | + $message['others']['responseBody'] = $content; |
| 210 | + } |
| 211 | + |
| 212 | + $encodedMessage = Json::encode($message) . "\n"; |
| 213 | + |
| 214 | + if (defined('DISABLE_ACCESS_OUT_LOG')) { |
| 215 | + $logFile = Yii::$app->getRuntimePath() . '/logs/access-out.log'; |
| 216 | + $logPath = dirname($logFile); |
| 217 | + if (!is_dir($logPath)) { |
| 218 | + FileHelper::createDirectory($logPath, 0775, true); |
| 219 | + } |
| 220 | + $text = '[' . date('Y-m-d H:i:s', time()) . ']' . $encodedMessage; |
| 221 | + $file = fopen($logFile, 'a'); |
| 222 | + fwrite($file, $text); |
| 223 | + fclose($file); |
| 224 | + |
| 225 | + return true; |
| 226 | + } |
| 227 | + |
| 228 | + if (PHP_SAPI === 'cli') { |
| 229 | + echo $encodedMessage; |
| 230 | + } else { |
| 231 | + file_put_contents('/var/run/phplog', $encodedMessage); |
| 232 | + } |
| 233 | + |
| 234 | + return true; |
| 235 | + } |
172 | 236 | } |
0 commit comments