@@ -56,6 +56,11 @@ class Request
56
56
*/
57
57
private $ _body ;
58
58
59
+ /**
60
+ * @var string
61
+ */
62
+ private $ _rawBody ;
63
+
59
64
/**
60
65
* @var RequestData
61
66
*/
@@ -126,21 +131,29 @@ public function setParams(RequestData $params): void
126
131
/**
127
132
* Returns the request body.
128
133
*
129
- * @return RequestData
134
+ * @return RequestData|string
130
135
*/
131
- public function getBody (): RequestData
136
+ public function getBody ()
132
137
{
133
- return $ this ->_body ;
138
+ return $ this ->_body != null
139
+ ? $ this ->_body
140
+ : $ this ->_rawBody ;
134
141
}
135
142
136
143
/**
137
144
* Sets the request body.
138
145
*
139
- * @param RequestData $body
146
+ * @param RequestData|string $body
140
147
*/
141
- public function setBody (RequestData $ body ): void
148
+ public function setBody ($ body ): void
142
149
{
143
- $ this ->_body = $ body ;
150
+ if ($ body instanceof RequestData) {
151
+ $ this ->_body = $ body ;
152
+ $ this ->_rawBody = null ;
153
+ } else {
154
+ $ this ->_body = null ;
155
+ $ this ->_rawBody = $ body ;
156
+ }
144
157
}
145
158
146
159
/**
@@ -204,16 +217,26 @@ public function isAjax(): bool
204
217
public function send ()
205
218
{
206
219
$ parameters = $ this ->getParams ()->toArray ();
207
- $ body = $ this ->getBody ()->toArray ();
220
+ $ body = $ this ->getBody ();
221
+
222
+ if ($ body instanceof RequestData) {
223
+ if ($ this ->_header ->getContentType () === "application/json " ) {
224
+ $ body = json_encode ($ body ->toArray ());
225
+ } else {
226
+ $ body = http_build_query ($ body ->toArray ());
227
+ }
228
+ }
208
229
209
230
$ path = $ this ->uri ->getUri ();
210
- $ path .= "? " . http_build_query ($ parameters );
231
+
232
+ if (count ($ parameters ) > 0 )
233
+ $ path .= "? " . http_build_query ($ parameters );
211
234
212
235
if (!($ curl = @\curl_init ($ path )))
213
236
throw new RequestException ("Unable to initialize cURL. " );
214
237
215
238
register_shutdown_function (function () use (&$ curl ) {
216
- curl_close ($ curl );
239
+ \ curl_close ($ curl );
217
240
});
218
241
219
242
$ headers = new ResponseHeader ();
@@ -223,10 +246,8 @@ public function send()
223
246
$ len = strlen ($ header );
224
247
$ header = explode (': ' , $ header , 2 );
225
248
226
- if (count ($ header ) < 2 )
227
- return $ len ;
228
-
229
- $ headers ->setField (trim ($ header [0 ]), trim ($ header [1 ]));
249
+ if (count ($ header ) >= 2 )
250
+ $ headers ->setField (trim ($ header [0 ]), trim ($ header [1 ]));
230
251
231
252
return $ len ;
232
253
});
@@ -237,17 +258,26 @@ public function send()
237
258
238
259
case RequestMethod::POST :
239
260
\curl_setopt ($ curl , CURLOPT_POST , true );
240
- \curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ( $ body) );
261
+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
241
262
break ;
242
263
243
264
case RequestMethod::DELETE :
244
265
\curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , "DELETE " );
245
- \curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ( $ body) );
266
+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
246
267
break ;
247
268
248
269
case RequestMethod::PUT :
249
270
\curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , "PUT " );
250
- \curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ($ body ));
271
+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
272
+ break ;
273
+
274
+ case RequestMethod::PATCH :
275
+ \curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , "PATCH " );
276
+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
277
+ break ;
278
+
279
+ case RequestMethod::HEAD :
280
+ \curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , "HEAD " );
251
281
break ;
252
282
253
283
default :
0 commit comments