@@ -56,6 +56,11 @@ class Request
5656 */
5757 private $ _body ;
5858
59+ /**
60+ * @var string
61+ */
62+ private $ _rawBody ;
63+
5964 /**
6065 * @var RequestData
6166 */
@@ -126,21 +131,29 @@ public function setParams(RequestData $params): void
126131 /**
127132 * Returns the request body.
128133 *
129- * @return RequestData
134+ * @return RequestData|string
130135 */
131- public function getBody (): RequestData
136+ public function getBody ()
132137 {
133- return $ this ->_body ;
138+ return $ this ->_body != null
139+ ? $ this ->_body
140+ : $ this ->_rawBody ;
134141 }
135142
136143 /**
137144 * Sets the request body.
138145 *
139- * @param RequestData $body
146+ * @param RequestData|string $body
140147 */
141- public function setBody (RequestData $ body ): void
148+ public function setBody ($ body ): void
142149 {
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+ }
144157 }
145158
146159 /**
@@ -204,16 +217,26 @@ public function isAjax(): bool
204217 public function send ()
205218 {
206219 $ 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+ }
208229
209230 $ path = $ this ->uri ->getUri ();
210- $ path .= "? " . http_build_query ($ parameters );
231+
232+ if (count ($ parameters ) > 0 )
233+ $ path .= "? " . http_build_query ($ parameters );
211234
212235 if (!($ curl = @\curl_init ($ path )))
213236 throw new RequestException ("Unable to initialize cURL. " );
214237
215238 register_shutdown_function (function () use (&$ curl ) {
216- curl_close ($ curl );
239+ \ curl_close ($ curl );
217240 });
218241
219242 $ headers = new ResponseHeader ();
@@ -223,10 +246,8 @@ public function send()
223246 $ len = strlen ($ header );
224247 $ header = explode (': ' , $ header , 2 );
225248
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 ]));
230251
231252 return $ len ;
232253 });
@@ -237,17 +258,26 @@ public function send()
237258
238259 case RequestMethod::POST :
239260 \curl_setopt ($ curl , CURLOPT_POST , true );
240- \curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ( $ body) );
261+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
241262 break ;
242263
243264 case RequestMethod::DELETE :
244265 \curl_setopt ($ curl , CURLOPT_CUSTOMREQUEST , "DELETE " );
245- \curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ( $ body) );
266+ \curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ body );
246267 break ;
247268
248269 case RequestMethod::PUT :
249270 \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 " );
251281 break ;
252282
253283 default :
0 commit comments