From b8f240d910b6b6fb8bad040fb281a13635460a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 12 Aug 2020 17:16:09 +0800 Subject: [PATCH] add clear --- wchat/common/HttpClient.php | 40 ++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index ebc1c9f..c25022b 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -615,6 +615,35 @@ class HttpClient } } + /** + * @param $url + * @param $params + * @return array|mixed|Result + * 上传文件 + */ + public function upload($url, $params) + { + try { + $output = $this->curlParse($url, $params); + if ($output === FALSE) { + return new Result(['code' => 500, 'message' => $output]); + } + [$header, $body, $status] = $this->explode($output); + if ($status != 200 && $status != 201) { + $data = new Result(['code' => $status, 'message' => $body, 'header' => $header]); + } else { + $data = $this->structure($body, $params, $header); + } + return $data; + } catch (\Throwable $exception) { + $response['code'] = 500; + $response['message'] = $exception->getMessage(); + $response['data'] = array_slice($exception->getTrace(), 0, 6); + $response['header'] = []; + return new Result($response); + } + } + /** * @param $url * @param $data @@ -631,14 +660,15 @@ class HttpClient if ($headers = $this->parseHeaderMat()) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } - if (!empty($this->agent)) { curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); } - - curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile()); - curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile()); - curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile()); + if ($this->getSslCertFile()) { + curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile()); + } + if ($this->getSslKeyFile()) { + curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile()); + } curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容