@@ -53,6 +53,7 @@ public function addPath($name, $argument = null) {
53
53
54
54
public function addPathVariable ($ variable ) {
55
55
$ this ->path [] = $ variable ;
56
+ return $ this ;
56
57
}
57
58
58
59
public function getUrl () {
@@ -85,18 +86,18 @@ public function dump() {
85
86
return $ this ;
86
87
}
87
88
88
- public function addFile ($ file_path ) {
89
- $ this ->files [] = $ filePath ;
89
+ public function addFile ($ field , $ file_path ) {
90
+ $ this ->files [$ field ] = $ file_path ;
91
+ return $ this ;
90
92
}
91
93
92
94
public function addFormParam ($ key , $ value ) {
93
95
$ this ->form_params [$ key ] = $ value ;
96
+ return $ this ;
94
97
}
95
98
96
99
public function call () {
97
-
98
100
$ client = new Client ();
99
- $ method = $ this ->method ;
100
101
101
102
try {
102
103
@@ -106,15 +107,8 @@ public function call() {
106
107
];
107
108
108
109
if (!empty ($ this ->files )) {
109
- foreach ($ this ->files as $ file ) {
110
- $ data ['multipart ' ][] = [
111
- 'name ' => basename ($ file ),
112
- 'contents ' => $ file
113
- ];
114
- }
115
- }
116
-
117
- if (!empty ($ this ->form_params )) {
110
+ $ data ['multipart ' ] = $ this ->buildMultiPart ();
111
+ } else if (!empty ($ this ->form_params )) {
118
112
$ data ['form_params ' ] = $ this ->form_params ;
119
113
}
120
114
@@ -126,7 +120,6 @@ public function call() {
126
120
} catch (RequestException $ e ) {
127
121
throw $ e ;
128
122
}
129
-
130
123
}
131
124
132
125
public function withHeaders ($ headers ) {
@@ -170,4 +163,22 @@ public function withParams($params) {
170
163
return $ this ;
171
164
}
172
165
166
+ private function buildMultiPart () {
167
+ $ multipart = array ();
168
+
169
+ foreach ($ this ->files as $ field => $ file ) {
170
+ $ multipart [] = [
171
+ 'name ' => $ field ,
172
+ 'contents ' => fopen ($ file , 'r ' )
173
+ ];
174
+ }
175
+ foreach ($ this ->form_params as $ param => $ value ) {
176
+ $ multipart [] = [
177
+ 'name ' => $ param ,
178
+ 'contents ' => $ value
179
+ ];
180
+ }
181
+ return $ multipart ;
182
+ }
183
+
173
184
}
0 commit comments