diff --git a/wx/WxClient.php b/wx/WxClient.php index b820326..105b0ea 100644 --- a/wx/WxClient.php +++ b/wx/WxClient.php @@ -3,6 +3,7 @@ namespace wchat; use Swoole\Coroutine\Http\Client; +use Swoole\Coroutine\System; class WxClient { @@ -116,6 +117,16 @@ class WxClient */ private function request($url, $data = []) { + if (function_exists('getIsCli') && getIsCli()) { + if (strpos($url, 'http://') !== FALSE) { + $url = str_replace('http://', '', $url); + } else if (strpos($url, 'https://') !== FALSE) { + $url = str_replace('https://', '', $url); + } + $explode = explode('/', $url); + $ip = System::gethostbyname(array_shift($explode)); + return $this->coroutine($ip, $url, $data); + } if ( strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0 @@ -123,10 +134,6 @@ class WxClient return $this->curl($url, $data); } - if (function_exists('getIsCli') && getIsCli()) { - return $this->coroutine($url, $data); - } - if ($this->isSSL) { return $this->curl('https://' . $this->host . '/' . $url, $data); } else { @@ -135,21 +142,20 @@ class WxClient } /** + * @param $ip * @param $url * @param array $data * @return array|mixed|Result * @throws \Exception - * * 使用swoole协程方式请求 */ - private function coroutine($url, $data = []) + private function coroutine($ip, $url, $data = []) { $_data = $this->paramEncode($data); if ($this->method == 'get' && is_array($_data)) { $url .= '?' . http_build_query($_data); } - - $client = $this->getClient($this->getHostPort(), $url, $data); + $client = $this->getClient($ip, $this->getHostPort(), $url, $data); if ($client->statusCode < 0) { throw new \Exception($client->errMsg); } @@ -179,14 +185,14 @@ class WxClient } /** + * @param $host * @param $port * @param $url * @param $data * @return Client */ - private function getClient($port, $url, $data) + private function getClient($host, $port, $url, $data) { - $host = $this->getHostIp(); $client = new Client($host, $port, $this->isSSL); if (!empty($this->agent)) { $this->header['User-Agent'] = $this->agent;