This commit is contained in:
2020-11-17 18:46:41 +08:00
parent 7a6ca96ac3
commit d7f3d4d65f
2 changed files with 104 additions and 106 deletions
+104 -105
View File
@@ -19,119 +19,118 @@ use Swoole\Coroutine\Http\Client as SClient;
class Client extends ClientAbstracts class Client extends ClientAbstracts
{ {
/** /**
* @param string $method * @param string $method
* @param $url * @param $url
* @param array $data * @param array $data
* @return array|mixed|Result * @return array|mixed|Result
* @throws Exception * @throws Exception
*/ */
public function request(string $method, $url, $data = []) public function request(string $method, $url, $data = [])
{ {
return $this->setMethod($method) return $this->setMethod($method)
->coroutine( ->coroutine(
$this->matchHost($url), $this->matchHost($url),
$this->paramEncode($data) $this->paramEncode($data)
); );
} }
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @return array|mixed|Result * @return array|mixed|Result
* @throws Exception * @throws Exception
* 使用swoole协程方式请求 * 使用swoole协程方式请求
*/ */
private function coroutine($url, $data = []) private function coroutine($url, $data = [])
{ {
try { try {
var_dump($url); $client = $this->generate_client($data, ...$url);
$client = $this->generate_client($data, ...$url); if ($client->statusCode < 0) {
if ($client->statusCode < 0) { throw new Exception($client->errMsg);
throw new Exception($client->errMsg); }
} $this->setData('');
$this->setData(''); $body = $this->resolve($client->getHeaders(), $client->body);
$body = $this->resolve($client->getHeaders(), $client->body); if (in_array($client->getStatusCode(), [200, 201])) {
if (in_array($client->getStatusCode(), [200, 201])) { return $this->structure($body, $data, $client->getHeaders());
return $this->structure($body, $data, $client->getHeaders()); }
} if (is_string($body)) {
if (is_string($body)) { $message = 'Request error code ' . $client->getStatusCode();
$message = 'Request error code ' . $client->getStatusCode(); } else {
} else { $message = $this->searchMessageByData($body);
$message = $this->searchMessageByData($body); }
} return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders());
return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders()); } catch (\Throwable $exception) {
} catch (\Throwable $exception) { return $this->fail(500, $exception->getMessage(), [
return $this->fail(500, $exception->getMessage(), [ 'file' => $exception->getFile(),
'file' => $exception->getFile(), 'line' => $exception->getLine()
'line' => $exception->getLine() ], []);
], []); }
} }
}
/** /**
* @param $data * @param $data
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path * @param $path
* @return SClient * @return SClient
*/ */
private function generate_client($data, $host, $isHttps, $path) private function generate_client($data, $host, $isHttps, $path)
{ {
$port = $isHttps ? 443 : $this->getPort(); if ($isHttps || $this->isSSL()) {
$client = new SClient($host, $port, $this->isSSL()); $client = new SClient($host, 443, $this->isSSL());
} else {
$client = new SClient($host, $this->getPort(), $this->isSSL());
}
if (strpos($path, '/') !== 0) { if (strpos($path, '/') !== 0) {
$path = '/' . $path; $path = '/' . $path;
} }
$client->set($this->settings());
if (!empty($this->getAgent())) {
$this->addHeader('User-Agent', $this->getAgent());
}
$client->set($this->settings()); $client->setHeaders($this->getHeader());
if (!empty($this->getAgent())) { $client->setMethod(strtoupper($this->getMethod()));
$this->addHeader('User-Agent', $this->getAgent()); if (strtolower($this->getMethod()) == self::GET && !empty($data)) {
} $path .= '?' . $data;
} else {
$this->setData($this->mergeParams($data));
}
$client->setHeaders($this->getHeader()); if (!empty($this->getData())) {
$client->setMethod(strtoupper($this->getMethod())); $client->setData($this->getData());
}
$client->execute($path);
$client->close();
return $client;
}
if (strtolower($this->getMethod()) == self::GET && !empty($data)) { /**
$path .= '?' . $data; * @return array
} else { */
$this->setData($this->mergeParams($data)); private function settings()
} {
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
if (!empty($this->getData())) { $params = [];
$client->setData($this->getData()); if ($this->getConnectTimeout() > 0) {
} $params['timeout'] = $this->getConnectTimeout();
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
$client->execute($path); $params['ssl_host_name'] = $this->getHost();
$client->close(); $params['ssl_cert_file'] = $this->getSslCertFile();
return $client; $params['ssl_key_file'] = $this->getSslKeyFile();
} $params['ssl_verify_peer'] = true;
$params['ssl_cafile'] = $sslCa;
/** return $params;
* @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;
}
} }
-1
View File
@@ -747,7 +747,6 @@ abstract class ClientAbstracts extends Component implements IClient
private function defaultString($string) private function defaultString($string)
{ {
$host = $this->getHost(); $host = $this->getHost();
$host .= ':' . $this->getPort();
if ($string == '/') { if ($string == '/') {
$string = ''; $string = '';
} else if (strpos($string, '/') !== 0) { } else if (strpos($string, '/') !== 0) {