From 798b55ac4fdcd1dd56a9b5b94703dc274e7d389b Mon Sep 17 00:00:00 2001 From: "zhao.zhang" Date: Wed, 15 Dec 2021 11:45:48 +0800 Subject: [PATCH] feature: add param retry for form upload --- src/Upyun/Api/Form.php | 37 +++++++++++++++++++++---------------- src/Upyun/Config.php | 5 +++++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Upyun/Api/Form.php b/src/Upyun/Api/Form.php index 549c639..f4086f8 100644 --- a/src/Upyun/Api/Form.php +++ b/src/Upyun/Api/Form.php @@ -23,22 +23,27 @@ public function upload($path, $stream, $params) 'timeout' => $this->config->timeout, ]); - $response = $client->request($method, $this->endpoint, array( - 'multipart' => array( - array( - 'name' => 'policy', - 'contents' => $policy, - ), - array( - 'name' => 'authorization', - 'contents' => $signature, - ), - array( - 'name' => 'file', - 'contents' => $stream, + for ($i = 0; $i < $this->config->retry; $i++) { + $response = $client->request($method, $this->endpoint, array( + 'multipart' => array( + array( + 'name' => 'policy', + 'contents' => $policy, + ), + array( + 'name' => 'authorization', + 'contents' => $signature, + ), + array( + 'name' => 'file', + 'contents' => $stream, + ) ) - ) - )); - return $response->getStatusCode() === 200; + )); + if ($response->getStatusCode() == 200) { + return true; + } + } + return false; } } diff --git a/src/Upyun/Config.php b/src/Upyun/Config.php index 1b99b6c..ebcf579 100644 --- a/src/Upyun/Config.php +++ b/src/Upyun/Config.php @@ -42,6 +42,11 @@ class Config */ public $sizeBoundary = 31457280; + /** + * @var int 支持 form 上传时重试 + */ + public $retry = 1; + /** * @var int 并行式断点续传的并发数 */