ea
This commit is contained in:
+15
-12
@@ -6,6 +6,7 @@ namespace Kiri;
|
||||
|
||||
/**
|
||||
* Class CurlClient
|
||||
*
|
||||
* @package Http\Handler\Client
|
||||
*/
|
||||
class CurlClient extends ClientAbstracts
|
||||
@@ -16,6 +17,7 @@ class CurlClient extends ClientAbstracts
|
||||
* @param string $method
|
||||
* @param string $path
|
||||
* @param array|string $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function request(string $method, string $path, array|string $params = []): void
|
||||
@@ -37,6 +39,7 @@ class CurlClient extends ClientAbstracts
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function getCurlHandler(string $path, string $method, $params): void
|
||||
@@ -88,6 +91,7 @@ class CurlClient extends ClientAbstracts
|
||||
* @param mixed $resource
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function do(mixed $resource, string $path, string $method): void
|
||||
@@ -134,6 +138,7 @@ class CurlClient extends ClientAbstracts
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function withCAInfo(string $path): static
|
||||
@@ -150,7 +155,7 @@ class CurlClient extends ClientAbstracts
|
||||
{
|
||||
$output = curl_exec($this->client);
|
||||
if ($output !== FALSE) {
|
||||
$this->explode($output);
|
||||
$this->explode($this->client, $output);
|
||||
} else {
|
||||
$this->setStatusCode(curl_errno($this->client));
|
||||
$this->setBody(curl_error($this->client));
|
||||
@@ -186,26 +191,24 @@ class CurlClient extends ClientAbstracts
|
||||
|
||||
/**
|
||||
* @param string $output
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
private function explode(string $output): void
|
||||
private function explode($ch, string $output): void
|
||||
{
|
||||
[$header, $body] = explode("\r\n\r\n", $output, 2);
|
||||
if ($header == 'HTTP/1.1 100 Continue') {
|
||||
[$header, $body] = explode("\r\n\r\n", $body, 2);
|
||||
}
|
||||
// 获取 HTTP 状态码
|
||||
$statusCode = +curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
$header = explode("\r\n", $header);
|
||||
$status = explode(' ', array_shift($header));
|
||||
|
||||
$statusCode = intval($status[1]);
|
||||
// 获取 header 的大小(不包括最后的 \r\n\r\n 分隔符)
|
||||
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$header = substr($output, 0, $headerSize);
|
||||
if (in_array($statusCode, [502, 404])) {
|
||||
$this->retry();
|
||||
} else {
|
||||
$this->setStatusCode($statusCode);
|
||||
$this->setBody($body);
|
||||
$this->setResponseHeader($header);
|
||||
$this->setBody(substr($output, $headerSize));
|
||||
$this->setResponseHeader(explode("\r\n", $header));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user