@@ -139,32 +139,39 @@ public function beforeSend($callback)
139
139
public function buildPostData ($ data )
140
140
{
141
141
$ binary_data = false ;
142
- if (is_array ($ data )) {
143
- // Return JSON-encoded string when the request's content-type is JSON.
144
- if (isset ($ this ->headers ['Content-Type ' ]) &&
145
- preg_match ($ this ->jsonPattern , $ this ->headers ['Content-Type ' ])) {
146
- $ data = \Curl \json_encode ($ data );
147
- } else {
148
- // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
149
- // CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
150
- // referenced.
151
- if (ArrayUtil::is_array_multidim ($ data )) {
152
- $ data = ArrayUtil::array_flatten_multidim ($ data );
153
- }
154
142
155
- // Modify array values to ensure any referenced files are properly handled depending on the support of
156
- // the @filename API or CURLFile usage. This also fixes the warning "curl_setopt(): The usage of the
157
- // @filename API for file uploading is deprecated. Please use the CURLFile class instead". Ignore
158
- // non-file values prefixed with the @ character.
159
- foreach ($ data as $ key => $ value ) {
160
- if (is_string ($ value ) && strpos ($ value , '@ ' ) === 0 && is_file (substr ($ value , 1 ))) {
161
- $ binary_data = true ;
162
- if (class_exists ('CURLFile ' )) {
163
- $ data [$ key ] = new \CURLFile (substr ($ value , 1 ));
164
- }
165
- } elseif ($ value instanceof \CURLFile) {
166
- $ binary_data = true ;
143
+ // Return JSON-encoded string when the request's content-type is JSON and the data is serializable.
144
+ if (isset ($ this ->headers ['Content-Type ' ]) &&
145
+ preg_match ($ this ->jsonPattern , $ this ->headers ['Content-Type ' ]) &&
146
+ (
147
+ is_array ($ data ) ||
148
+ (
149
+ is_object ($ data ) &&
150
+ interface_exists ('JsonSerializable ' , false ) &&
151
+ $ data instanceof \JsonSerializable
152
+ )
153
+ )) {
154
+ $ data = \Curl \json_encode ($ data );
155
+ } elseif (is_array ($ data )) {
156
+ // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
157
+ // CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
158
+ // referenced.
159
+ if (ArrayUtil::is_array_multidim ($ data )) {
160
+ $ data = ArrayUtil::array_flatten_multidim ($ data );
161
+ }
162
+
163
+ // Modify array values to ensure any referenced files are properly handled depending on the support of
164
+ // the @filename API or CURLFile usage. This also fixes the warning "curl_setopt(): The usage of the
165
+ // @filename API for file uploading is deprecated. Please use the CURLFile class instead". Ignore
166
+ // non-file values prefixed with the @ character.
167
+ foreach ($ data as $ key => $ value ) {
168
+ if (is_string ($ value ) && strpos ($ value , '@ ' ) === 0 && is_file (substr ($ value , 1 ))) {
169
+ $ binary_data = true ;
170
+ if (class_exists ('CURLFile ' )) {
171
+ $ data [$ key ] = new \CURLFile (substr ($ value , 1 ));
167
172
}
173
+ } elseif ($ value instanceof \CURLFile) {
174
+ $ binary_data = true ;
168
175
}
169
176
}
170
177
}
0 commit comments