From 5e978d574e619dfe64204b18085e4a17766a1807 Mon Sep 17 00:00:00 2001 From: Hanson Date: Sat, 12 Oct 2019 10:36:29 +0800 Subject: [PATCH] Update Http.php --- src/Http.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Http.php b/src/Http.php index 3526381..6dae56c 100644 --- a/src/Http.php +++ b/src/Http.php @@ -133,11 +133,30 @@ public function upload($url, array $queries = [], array $files = [], array $form } foreach ($form as $name => $contents) { - $multipart[] = compact('name', 'contents'); + $multipart = array_merge($multipart, $this->normalizeMultipartField($name, $contents)); } return $this->request('POST', $url, ['query' => $queries, 'multipart' => $multipart]); } + + /** + * @param string $name + * @param mixed $contents + * + * @return array + */ + public function normalizeMultipartField(string $name, $contents) { + $field = []; + if (!is_array($contents)) { + return [compact('name', 'contents')]; + } else { + foreach ($contents as $key => $value) { + $key = sprintf('%s[%s]', $name, $key); + $field = array_merge($field, is_array($value) ? $this->normalizeMultipartField($key, $value) : [['name' => $key, 'contents' => $value]]); + } + } + return $field; + } private function fileToMultipart($file) {