From 6c94ecc353ecdbde463d92b400099944efa5c87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 24 Aug 2020 11:59:31 +0800 Subject: [PATCH] add clear --- wchat/common/HttpClient.php | 73 +++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 9dd4af8..e0ec02a 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -653,44 +653,63 @@ class HttpClient */ private function curlParse($url, $data) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data)); - curl_setopt($ch, CURLOPT_TIMEOUT, 120);// 超时设置 - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_NOBODY, FALSE); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // 超时设置 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 -// curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); - if ($headers = $this->parseHeaderMat()) { - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $this->createRequestUrl($url, $data)); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($this->method)); + curl_setopt($curl, CURLOPT_TIMEOUT, 120);// 超时设置 + curl_setopt($curl, CURLOPT_HEADER, true); + curl_setopt($curl, CURLOPT_NOBODY, FALSE); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); // 超时设置 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //返回内容 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // 跟踪重定向 +// curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); + $this->parseHeader($curl); + $this->parseSslFile($curl); if (!empty($this->agent)) { - curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); - } - if (file_exists($cert = $this->getSslCertFile())) { - curl_setopt($ch, CURLOPT_SSLCERT, $cert); - } - if (file_exists($key = $this->getSslKeyFile())) { - curl_setopt($ch, CURLOPT_SSLKEY, $key); + curl_setopt($curl, CURLOPT_USERAGENT, $this->agent); } if ($this->method == self::POST) { - curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POST, 1); } - if ($this->method != self::GET) { - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } - - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method)); - $output = curl_exec($ch); + $output = curl_exec($curl); if ($output === false) { - throw new Exception(curl_error($ch)); + throw new Exception(curl_error($curl)); } - curl_close($ch); + curl_close($curl); return $output; } + /** + * @param $curl + */ + private function parseSslFile($curl) + { + $key = $this->getSslKeyFile(); + $cert = $this->getSslCertFile(); + if (!empty($cert) && file_exists($cert)) { + curl_setopt($curl, CURLOPT_SSLCERT, $cert); + } + if (!empty($key) && file_exists($key)) { + curl_setopt($curl, CURLOPT_SSLKEY, $key); + } + } + + /** + * @param $curl + */ + private function parseHeader($curl) + { + $headers = $this->parseHeaderMat(); + if (empty($headers)) { + return; + } + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + } + + /** * @param $output * @return array