diff --git a/HttpServer/Client/Client.php b/HttpServer/Client/Client.php index 5963c651..dad08498 100644 --- a/HttpServer/Client/Client.php +++ b/HttpServer/Client/Client.php @@ -19,119 +19,118 @@ use Swoole\Coroutine\Http\Client as SClient; class Client extends ClientAbstracts { - /** - * @param string $method - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - */ - public function request(string $method, $url, $data = []) - { - return $this->setMethod($method) - ->coroutine( - $this->matchHost($url), - $this->paramEncode($data) - ); - } + /** + * @param string $method + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + */ + public function request(string $method, $url, $data = []) + { + return $this->setMethod($method) + ->coroutine( + $this->matchHost($url), + $this->paramEncode($data) + ); + } - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - * 使用swoole协程方式请求 - */ - private function coroutine($url, $data = []) - { - try { - var_dump($url); - $client = $this->generate_client($data, ...$url); - if ($client->statusCode < 0) { - throw new Exception($client->errMsg); - } - $this->setData(''); - $body = $this->resolve($client->getHeaders(), $client->body); - if (in_array($client->getStatusCode(), [200, 201])) { - return $this->structure($body, $data, $client->getHeaders()); - } - if (is_string($body)) { - $message = 'Request error code ' . $client->getStatusCode(); - } else { - $message = $this->searchMessageByData($body); - } - return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders()); - } catch (\Throwable $exception) { - return $this->fail(500, $exception->getMessage(), [ - 'file' => $exception->getFile(), - 'line' => $exception->getLine() - ], []); - } - } + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + * 使用swoole协程方式请求 + */ + private function coroutine($url, $data = []) + { + try { + $client = $this->generate_client($data, ...$url); + if ($client->statusCode < 0) { + throw new Exception($client->errMsg); + } + $this->setData(''); + $body = $this->resolve($client->getHeaders(), $client->body); + if (in_array($client->getStatusCode(), [200, 201])) { + return $this->structure($body, $data, $client->getHeaders()); + } + if (is_string($body)) { + $message = 'Request error code ' . $client->getStatusCode(); + } else { + $message = $this->searchMessageByData($body); + } + return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders()); + } catch (\Throwable $exception) { + return $this->fail(500, $exception->getMessage(), [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine() + ], []); + } + } - /** - * @param $data - * @param $host - * @param $isHttps - * @param $path - * @return SClient - */ - private function generate_client($data, $host, $isHttps, $path) - { - $port = $isHttps ? 443 : $this->getPort(); - $client = new SClient($host, $port, $this->isSSL()); + /** + * @param $data + * @param $host + * @param $isHttps + * @param $path + * @return SClient + */ + private function generate_client($data, $host, $isHttps, $path) + { + if ($isHttps || $this->isSSL()) { + $client = new SClient($host, 443, $this->isSSL()); + } else { + $client = new SClient($host, $this->getPort(), $this->isSSL()); + } - if (strpos($path, '/') !== 0) { - $path = '/' . $path; - } + if (strpos($path, '/') !== 0) { + $path = '/' . $path; + } + $client->set($this->settings()); + if (!empty($this->getAgent())) { + $this->addHeader('User-Agent', $this->getAgent()); + } - $client->set($this->settings()); - if (!empty($this->getAgent())) { - $this->addHeader('User-Agent', $this->getAgent()); - } + $client->setHeaders($this->getHeader()); + $client->setMethod(strtoupper($this->getMethod())); + if (strtolower($this->getMethod()) == self::GET && !empty($data)) { + $path .= '?' . $data; + } else { + $this->setData($this->mergeParams($data)); + } - $client->setHeaders($this->getHeader()); - $client->setMethod(strtoupper($this->getMethod())); + if (!empty($this->getData())) { + $client->setData($this->getData()); + } + $client->execute($path); + $client->close(); + return $client; + } - if (strtolower($this->getMethod()) == self::GET && !empty($data)) { - $path .= '?' . $data; - } else { - $this->setData($this->mergeParams($data)); - } + /** + * @return array + */ + private function settings() + { + $sslCert = $this->getSslCertFile(); + $sslKey = $this->getSslKeyFile(); + $sslCa = $this->getCa(); - if (!empty($this->getData())) { - $client->setData($this->getData()); - } + $params = []; + if ($this->getConnectTimeout() > 0) { + $params['timeout'] = $this->getConnectTimeout(); + } + if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { + return $params; + } - $client->execute($path); - $client->close(); - return $client; - } + $params['ssl_host_name'] = $this->getHost(); + $params['ssl_cert_file'] = $this->getSslCertFile(); + $params['ssl_key_file'] = $this->getSslKeyFile(); + $params['ssl_verify_peer'] = true; + $params['ssl_cafile'] = $sslCa; - /** - * @return array - */ - private function settings() - { - $sslCert = $this->getSslCertFile(); - $sslKey = $this->getSslKeyFile(); - $sslCa = $this->getCa(); - - $params = []; - if ($this->getConnectTimeout() > 0) { - $params['timeout'] = $this->getConnectTimeout(); - } - if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { - return $params; - } - - $params['ssl_host_name'] = $this->getHost(); - $params['ssl_cert_file'] = $this->getSslCertFile(); - $params['ssl_key_file'] = $this->getSslKeyFile(); - $params['ssl_verify_peer'] = true; - $params['ssl_cafile'] = $sslCa; - - return $params; - } + return $params; + } } diff --git a/HttpServer/Client/ClientAbstracts.php b/HttpServer/Client/ClientAbstracts.php index c7802011..43077856 100644 --- a/HttpServer/Client/ClientAbstracts.php +++ b/HttpServer/Client/ClientAbstracts.php @@ -747,7 +747,6 @@ abstract class ClientAbstracts extends Component implements IClient private function defaultString($string) { $host = $this->getHost(); - $host .= ':' . $this->getPort(); if ($string == '/') { $string = ''; } else if (strpos($string, '/') !== 0) {