This commit is contained in:
2020-11-18 14:15:25 +08:00
parent d5bd5bd269
commit e945e33601
2 changed files with 184 additions and 182 deletions
+1
View File
@@ -19,6 +19,7 @@ abstract class ClientAbstracts extends Component implements IClient
{ {
const POST = 'post'; const POST = 'post';
const UPLOAD = 'upload';
const GET = 'get'; const GET = 'get';
const DELETE = 'delete'; const DELETE = 'delete';
const OPTIONS = 'options'; const OPTIONS = 'options';
+183 -182
View File
@@ -13,218 +13,219 @@ use Exception;
class Curl extends ClientAbstracts class Curl extends ClientAbstracts
{ {
/** /**
* @param $path * @param $path
* @param $method * @param $method
* @param array $params * @param array $params
* @return bool|string * @return bool|string
* @throws Exception * @throws Exception
*/ */
public function request($method, $path, $params = []) public function request($method, $path, $params = [])
{ {
if ($method == self::GET) { if ($method == self::GET) {
$path = $this->joinGetParams($path, $params); $path = $this->joinGetParams($path, $params);
} }
return $this->execute($this->getCurlHandler($path, $method, $params)); return $this->execute($this->getCurlHandler($path, $method, $params));
} }
/** /**
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
* @return mixed|resource * @return mixed|resource
* @throws Exception * @throws Exception
*/ */
private function getCurlHandler($path, $method, $params) private function getCurlHandler($path, $method, $params)
{ {
[$host, $isHttps, $path] = $this->matchHost($path); [$host, $isHttps, $path] = $this->matchHost($path);
$resource = $this->do(curl_init($host . $path), $host . $path, $method); $resource = $this->do(curl_init($host . $path), $host . $path, $method);
if ($method === self::POST && !empty($params)) { if ($method === self::POST && !empty($params)) {
curl_setopt($resource, CURLOPT_POSTFIELDS, HttpParse::parse($params)); curl_setopt($resource, CURLOPT_POSTFIELDS, HttpParse::parse($params));
} } else if ($method === self::UPLOAD) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $params);
}
if ($isHttps !== false) { if ($isHttps !== false) {
return $this->curlHandlerSslSet($resource); return $this->curlHandlerSslSet($resource);
} }
return $resource; return $resource;
} }
/** /**
* @param $path * @param $path
* @param $params * @param $params
* @return mixed|resource * @return mixed|resource
* @throws Exception * @throws Exception
*/ */
public function upload($path, $params = []) public function upload($path, $params = [])
{ {
[$host, $isHttps, $path] = $this->matchHost($path); [$host, $isHttps, $path] = $this->matchHost($path);
$resource = $this->do(curl_init($host . $path), $host . $path, self::POST); $resource = $this->do(curl_init($host . $path), $host . $path, self::POST);
var_dump($params); var_dump($params);
curl_setopt($resource, CURLOPT_POSTFIELDS, $params); curl_setopt($resource, CURLOPT_POSTFIELDS, $params);
if ($isHttps !== false) { if ($isHttps !== false) {
return $this->execute($this->curlHandlerSslSet($resource)); return $this->execute($this->curlHandlerSslSet($resource));
} }
return $this->execute($resource); return $this->execute($resource);
} }
/** /**
* @param $resource * @param $resource
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
private function curlHandlerSslSet($resource) private function curlHandlerSslSet($resource)
{ {
if (!empty($this->ssl_key)) { if (!empty($this->ssl_key)) {
if (!file_exists($this->ssl_key)) { if (!file_exists($this->ssl_key)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLKEY, $this->getSslKeyFile()); curl_setopt($resource, CURLOPT_SSLKEY, $this->getSslKeyFile());
} }
if (!empty($this->ssl_cert)) { if (!empty($this->ssl_cert)) {
if (!!file_exists($this->ssl_cert)) { if (!!file_exists($this->ssl_cert)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile()); curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile());
} }
return $resource; return $resource;
} }
/** /**
* @param $resource * @param $resource
* @param $path * @param $path
* @param $method * @param $method
* @return resource * @return resource
* @throws Exception * @throws Exception
*/ */
private function do($resource, $path, $method) private function do($resource, $path, $method)
{ {
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置
curl_setopt($resource, CURLOPT_HEADER, true); curl_setopt($resource, CURLOPT_HEADER, true);
curl_setopt($resource, CURLOPT_FAILONERROR, true); curl_setopt($resource, CURLOPT_FAILONERROR, true);
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
curl_setopt($resource, CURLOPT_SSL_FALSESTART, true); curl_setopt($resource, CURLOPT_SSL_FALSESTART, true);
curl_setopt($resource, CURLOPT_FORBID_REUSE, false); curl_setopt($resource, CURLOPT_FORBID_REUSE, false);
curl_setopt($resource, CURLOPT_FRESH_CONNECT, false); curl_setopt($resource, CURLOPT_FRESH_CONNECT, false);
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent());
} }
curl_setopt($resource, CURLOPT_NOBODY, FALSE); curl_setopt($resource, CURLOPT_NOBODY, FALSE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_POST, 1);
}
if ($method === self::POST) { curl_setopt($resource, CURLOPT_URL, $path);
curl_setopt($resource, CURLOPT_POST, 1); curl_setopt($resource, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($resource, CURLOPT_URL, $path); return $resource;
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, $method); }
return $resource;
}
/** /**
* @param $curl * @param $curl
* @return bool|string * @return bool|string
* @throws Exception * @throws Exception
*/ */
private function execute($curl) private function execute($curl)
{ {
$output = curl_exec($curl); $output = curl_exec($curl);
var_dump($output); var_dump($output);
if ($output === false) { if ($output === false) {
return $this->fail(400, curl_error($curl)); return $this->fail(400, curl_error($curl));
} }
return $this->parseResponse($curl, $output); return $this->parseResponse($curl, $output);
} }
/** /**
* @param $curl * @param $curl
* @param $output * @param $output
* @param array $params * @param array $params
* @return array|Result|mixed * @return array|Result|mixed
* @throws Exception * @throws Exception
*/ */
private function parseResponse($curl, $output, $params = []) private function parseResponse($curl, $output, $params = [])
{ {
curl_close($curl); curl_close($curl);
if ($output === FALSE) { if ($output === FALSE) {
return $this->fail(500, $output); return $this->fail(500, $output);
} }
[$header, $body, $status] = $this->explode($output); [$header, $body, $status] = $this->explode($output);
if ($status != 200 && $status != 201) { if ($status != 200 && $status != 201) {
$data = $this->fail($status, $body, [], $header); $data = $this->fail($status, $body, [], $header);
} else { } else {
$data = $this->structure($body, $params, $header); $data = $this->structure($body, $params, $header);
} }
return $data; return $data;
} }
/** /**
* @param $output * @param $output
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function explode($output) private function explode($output)
{ {
if (empty($output) || strpos($output, "\r\n\r\n") === false) { if (empty($output) || strpos($output, "\r\n\r\n") === false) {
throw new Exception('Get data null.'); throw new Exception('Get data null.');
} }
[$header, $body] = explode("\r\n\r\n", $output, 2); [$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') { if ($header == 'HTTP/1.1 100 Continue') {
[$header, $body] = explode("\r\n\r\n", $body, 2); [$header, $body] = explode("\r\n\r\n", $body, 2);
} }
$header = explode("\r\n", $header); $header = explode("\r\n", $header);
$status = (int)explode(' ', trim($header[0]))[1]; $status = (int)explode(' ', trim($header[0]))[1];
$header = $this->headerFormat($header); $header = $this->headerFormat($header);
return [$header, $this->resolve($header, $body), $status]; return [$header, $this->resolve($header, $body), $status];
} }
/** /**
* @param $headers * @param $headers
* @return array * @return array
*/ */
private function headerFormat($headers) private function headerFormat($headers)
{ {
$_tmp = []; $_tmp = [];
foreach ($headers as $key => $val) { foreach ($headers as $key => $val) {
$trim = explode(': ', trim($val)); $trim = explode(': ', trim($val));
$_tmp[strtolower($trim[0])] = $trim[1] ?? ''; $_tmp[strtolower($trim[0])] = $trim[1] ?? '';
} }
return $_tmp; return $_tmp;
} }
/** /**
* @return array * @return array
*/ */
private function parseHeaderMat() private function parseHeaderMat()
{ {
$headers = []; $headers = [];
foreach ($this->getHeader() as $key => $val) { foreach ($this->getHeader() as $key => $val) {
$headers[$key] = $key . ': ' . $val; $headers[$key] = $key . ': ' . $val;
} }
var_dump($headers); var_dump($headers);
return array_values($headers); return array_values($headers);
} }
} }