diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index e5b7a52..859da63 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -24,6 +24,7 @@ class HttpClient private $ssl_cert_file = ''; private $ssl_key_file = ''; + private $ca = ''; private $port = 80; @@ -42,6 +43,25 @@ class HttpClient { } + /** + * @return string + */ + public function getCa(): string + { + return $this->ca; + } + + /** + * @param string $ca + * @return HttpClient + */ + public function setCa(string $ca): HttpClient + { + $this->ca = $ca; + return $this; + } + + public function setData($data) { $this->_data = $data; @@ -221,18 +241,22 @@ class HttpClient private function request($url, $data = []) { $data = $this->paramEncode($data); - if ($this->use_swoole === false) { - return $this->useCurl($url, $data); - } else if ($this->use_swoole) { - return $this->coroutine($this->parseUrlHost($url), $url, $data); + if ($this->use_swoole && $this->isCli()) { + [$url, $host] = $this->parseUrlHost($url); + return $this->coroutine($host, $url, $data); } else { - if (function_exists('getIsCli') && getIsCli()) { - return $this->coroutine($this->parseUrlHost($url), $url, $data); - } return $this->useCurl($url, $data); } } + /** + * @return bool + */ + private function isCli() + { + return function_exists('getIsCli') && getIsCli(); + } + /** * @param $url * @param $data @@ -252,19 +276,22 @@ class HttpClient /** * @param $url - * @return string + * @return array */ - private function parseUrlHost(&$url) + private function parseUrlHost($url) { - if (!$this->isHttps($url) && !$this->isHttp($url)) { - $url = $this->host . '/' . $url; + if (strpos($this->host, $url) === false) { + $url = rtrim($this->host, '/') . '/' . ltrim($url, '/'); } - $host = $this->cutRequestUrl($url); + if (!$this->isHttp($url) && !$this->isHttps($url)) { + $url = ($this->isSSL ? 'https://' : 'http://') . $url; + } + [$host, $url] = $this->cutRequestUrl($url); if ($this->checkIsIp($host)) { - return $host; + return [$url, $host]; } $this->host = $host; - return System::gethostbyname($host); + return [$url, System::gethostbyname($host)]; } /** @@ -353,7 +380,12 @@ class HttpClient unset($this->_data); if ($client->getStatusCode() != 200) { - return new Result(['code' => $client->getStatusCode(), 'message' => $this->searchMessageByData($body), 'data' => $body]); + if (is_string($body)) { + $message = 'Request error code ' . $client->getStatusCode(); + } else { + $message = $this->searchMessageByData($body); + } + return new Result(['code' => $client->getStatusCode(), 'message' => $message, 'data' => $body]); } return $this->structure($body, $data, $header); @@ -391,7 +423,13 @@ class HttpClient private function generate_client($host, $url, $data) { $client = new SClient($host, $this->getHostPort(), $this->isSSL); - echo $host . ':' . $this->getHostPort() . $url . PHP_EOL; + $client->set([ + 'ssl_host_name' => $this->host, + 'ssl_cert_file' => $this->getSslCertFile(), + 'ssl_key_file' => $this->getSslKeyFile(), + 'ssl_verify_peer' => true, + 'ssl_cafile' => $this->getCa(), + ]); if (!empty($this->agent)) { $this->header['User-Agent'] = $this->agent; } @@ -461,7 +499,6 @@ class HttpClient if ($this->method == self::POST) { curl_setopt($ch, CURLOPT_POST, 1); } - echo $url . PHP_EOL; if ($this->method != self::GET) { if (!empty($this->_data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);