From af9e25e7de7de228a19ddd7cef8c7fb253861512 Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 15:26:45 +0800 Subject: [PATCH 1/6] post request enhance post request support multi-file upload, support file upload and send complex array parameters --- library/Requests.php | 49 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index bb266189c..57587ee61 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -258,15 +258,42 @@ public static function trace($url, $headers = array(), $options = array()) { * @param string $url * @param array $headers * @param array $data + * @param array $files * @param array $options * @return Requests_Response */ /** - * Send a POST request - */ - public static function post($url, $headers = array(), $data = array(), $options = array()) { - return self::request($url, $headers, $data, self::POST, $options); - } + * Send a POST request/Support for multiple files, complex array parameters + */ + public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ + if (count($files)) { + + // replacing file paths with curlFile Object + array_walk($files, function ($filePath, $key) use (&$data) { + if (is_array($filePath)) { + array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { + $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); + }); + } else { + $data[ $key ] = new \CURLFile(realpath($filePath)); + } + }); + + self::httpBuildQuery($data); + + // starting to add a hook to attach file to request + $hooks = new \Requests_Hooks(); + $hooks->register('curl.before_send', function ($fp) use ($data) { + curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); + curl_setopt($fp, CURLOPT_POSTFIELDS, $data); + }); + $options = ['hooks' => $hooks]; + // no need to set the body, it's taken care of by hooks + $data = []; + } + + return self::request($url, $headers, $data, self::POST, $options); + } /** * Send a PUT request */ @@ -977,4 +1004,16 @@ public static function match_domain($host, $reference) { return false; } + + protected static function httpBuildQuery(array &$data, $key = '', $value = null) { + foreach ($value ?? $data as $k => $v) { + if (is_array($v)) { + self::httpBuildQuery($data, "{$key}[{$k}]", $v); + }else{ + $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; + $data[$cur_key] = $v; + } + unset($data[$k]); + } + } } From 13463b011d1d89f82bb1e439aef1e14e7ed8ba1b Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 15:28:26 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/Requests.php | 74 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index 57587ee61..6dd2a4e43 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -265,35 +265,35 @@ public static function trace($url, $headers = array(), $options = array()) { /** * Send a POST request/Support for multiple files, complex array parameters */ - public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ - if (count($files)) { - - // replacing file paths with curlFile Object - array_walk($files, function ($filePath, $key) use (&$data) { - if (is_array($filePath)) { - array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { - $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); - }); - } else { - $data[ $key ] = new \CURLFile(realpath($filePath)); - } - }); - - self::httpBuildQuery($data); - - // starting to add a hook to attach file to request - $hooks = new \Requests_Hooks(); - $hooks->register('curl.before_send', function ($fp) use ($data) { - curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); - curl_setopt($fp, CURLOPT_POSTFIELDS, $data); - }); - $options = ['hooks' => $hooks]; - // no need to set the body, it's taken care of by hooks - $data = []; - } - - return self::request($url, $headers, $data, self::POST, $options); + public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ + if (count($files)) { + + // replacing file paths with curlFile Object + array_walk($files, function ($filePath, $key) use (&$data) { + if (is_array($filePath)) { + array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { + $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); + }); + } else { + $data[ $key ] = new \CURLFile(realpath($filePath)); + } + }); + + self::httpBuildQuery($data); + + // starting to add a hook to attach file to request + $hooks = new \Requests_Hooks(); + $hooks->register('curl.before_send', function ($fp) use ($data) { + curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); + curl_setopt($fp, CURLOPT_POSTFIELDS, $data); + }); + $options = ['hooks' => $hooks]; + // no need to set the body, it's taken care of by hooks + $data = []; } + + return self::request($url, $headers, $data, self::POST, $options); + } /** * Send a PUT request */ @@ -1005,15 +1005,15 @@ public static function match_domain($host, $reference) { return false; } - protected static function httpBuildQuery(array &$data, $key = '', $value = null) { - foreach ($value ?? $data as $k => $v) { - if (is_array($v)) { - self::httpBuildQuery($data, "{$key}[{$k}]", $v); - }else{ - $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; - $data[$cur_key] = $v; - } - unset($data[$k]); + protected static function httpBuildQuery(array &$data, $key = '', $value = null) { + foreach ($value ?? $data as $k => $v) { + if (is_array($v)) { + self::httpBuildQuery($data, "{$key}[{$k}]", $v); + }else{ + $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; + $data[$cur_key] = $v; } + unset($data[$k]); } + } } From 160415021cb7316ad784fa0fe5fb8997a9dc6470 Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 15:39:38 +0800 Subject: [PATCH 3/6] fix files object array --- library/Requests.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index 6dd2a4e43..5ad3d9170 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -267,20 +267,18 @@ public static function trace($url, $headers = array(), $options = array()) { */ public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ if (count($files)) { - + self::httpBuildQuery($data); // replacing file paths with curlFile Object array_walk($files, function ($filePath, $key) use (&$data) { if (is_array($filePath)) { array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { - $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); + $data["{$key}[{$subKey}]"] = new \CURLFile(realpath($subFilePath)); }); } else { $data[ $key ] = new \CURLFile(realpath($filePath)); } }); - self::httpBuildQuery($data); - // starting to add a hook to attach file to request $hooks = new \Requests_Hooks(); $hooks->register('curl.before_send', function ($fp) use ($data) { @@ -294,6 +292,7 @@ public static function post($url, $headers = [], $data = [], $files = [], $optio return self::request($url, $headers, $data, self::POST, $options); } + /** * Send a PUT request */ From aa103a180aa7dbef3dd1810bae73d52a0fe7e790 Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 15:51:59 +0800 Subject: [PATCH 4/6] fix httpBuildQuery function --- library/Requests.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index 5ad3d9170..0e328829a 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -1006,13 +1006,13 @@ public static function match_domain($host, $reference) { protected static function httpBuildQuery(array &$data, $key = '', $value = null) { foreach ($value ?? $data as $k => $v) { + $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; if (is_array($v)) { - self::httpBuildQuery($data, "{$key}[{$k}]", $v); + self::httpBuildQuery($data, "{$cur_key}", $v); + unset($data[$k]); }else{ - $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; $data[$cur_key] = $v; } - unset($data[$k]); } } } From 06773bf0aa4ef7e8d3dbc02d578f9b286aa6c67e Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 18:00:00 +0800 Subject: [PATCH 5/6] test fix --- tests/Transport/Base.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Transport/Base.php b/tests/Transport/Base.php index 566e09fad..4f5604228 100644 --- a/tests/Transport/Base.php +++ b/tests/Transport/Base.php @@ -160,7 +160,7 @@ public function testTRACE() { public function testRawPOST() { $data = 'test'; - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions()); + $request = Requests::post(httpbin('/post'), array(), $data,['fiilename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions()); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); @@ -169,7 +169,7 @@ public function testRawPOST() { public function testFormPost() { $data = 'test=true&test2=test'; - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions()); + $request = Requests::post(httpbin('/post'), array(), $data,['fiilename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions()); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); @@ -181,7 +181,7 @@ public function testPOSTWithArray() { 'test' => 'true', 'test2' => 'test', ); - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions()); + $request = Requests::post(httpbin('/post'), array(), $data,['fiilename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions()); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); @@ -196,7 +196,7 @@ public function testPOSTWithNestedData() { 'test4' => 'test-too', ), ); - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions()); + $request = Requests::post(httpbin('/post'), array(), $data,['fiilename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions()); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); @@ -828,7 +828,7 @@ public function testReusableTransport() { public function testQueryDataFormat() { $data = array('test' => 'true', 'test2' => 'test'); - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'query'))); + $request = Requests::post(httpbin('/post'), array(), $data,['filename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions(array('data_format' => 'query'))); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); @@ -838,7 +838,7 @@ public function testQueryDataFormat() { public function testBodyDataFormat() { $data = array('test' => 'true', 'test2' => 'test'); - $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'body'))); + $request = Requests::post(httpbin('/post'), array(), $data,['filename' => tempnam(sys_get_temp_dir(), 'RLT')], $this->getOptions(array('data_format' => 'body'))); $this->assertEquals(200, $request->status_code); $result = json_decode($request->body, true); From 40fdfe4466d5dbdbd96963f0c5a1f7082174d09c Mon Sep 17 00:00:00 2001 From: jianmingLee Date: Fri, 3 Aug 2018 18:05:07 +0800 Subject: [PATCH 6/6] Compatible with low version PHP --- library/Requests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Requests.php b/library/Requests.php index 0e328829a..38551fc00 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -265,7 +265,7 @@ public static function trace($url, $headers = array(), $options = array()) { /** * Send a POST request/Support for multiple files, complex array parameters */ - public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ + public static function post($url, $headers = [], $data = [], $files = [], $options = []){ if (count($files)) { self::httpBuildQuery($data); // replacing file paths with curlFile Object