@@ -265,35 +265,35 @@ discard block |
||
| 265 | 265 | /** |
| 266 | 266 | * Send a POST request/Support for multiple files, complex array parameters |
| 267 | 267 | */ |
| 268 | - public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ |
|
| 269 | - if (count($files)) { |
|
| 270 | - |
|
| 271 | - // replacing file paths with curlFile Object |
|
| 272 | - array_walk($files, function ($filePath, $key) use (&$data) { |
|
| 273 | - if (is_array($filePath)) { |
|
| 274 | - array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { |
|
| 275 | - $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); |
|
| 276 | - }); |
|
| 277 | - } else { |
|
| 278 | - $data[ $key ] = new \CURLFile(realpath($filePath)); |
|
| 279 | - } |
|
| 280 | - }); |
|
| 281 | - |
|
| 282 | - self::httpBuildQuery($data); |
|
| 283 | - |
|
| 284 | - // starting to add a hook to attach file to request |
|
| 285 | - $hooks = new \Requests_Hooks(); |
|
| 286 | - $hooks->register('curl.before_send', function ($fp) use ($data) { |
|
| 287 | - curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); |
|
| 288 | - curl_setopt($fp, CURLOPT_POSTFIELDS, $data); |
|
| 289 | - }); |
|
| 290 | - $options = ['hooks' => $hooks]; |
|
| 291 | - // no need to set the body, it's taken care of by hooks |
|
| 292 | - $data = []; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - return self::request($url, $headers, $data, self::POST, $options); |
|
| 296 | - } |
|
| 268 | + public static function post($url, $headers = [], $data = [], $files = [], $options = []) : \Requests_Response{ |
|
| 269 | + if (count($files)) { |
|
| 270 | + |
|
| 271 | + // replacing file paths with curlFile Object |
|
| 272 | + array_walk($files, function ($filePath, $key) use (&$data) { |
|
| 273 | + if (is_array($filePath)) { |
|
| 274 | + array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { |
|
| 275 | + $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); |
|
| 276 | + }); |
|
| 277 | + } else { |
|
| 278 | + $data[ $key ] = new \CURLFile(realpath($filePath)); |
|
| 279 | + } |
|
| 280 | + }); |
|
| 281 | + |
|
| 282 | + self::httpBuildQuery($data); |
|
| 283 | + |
|
| 284 | + // starting to add a hook to attach file to request |
|
| 285 | + $hooks = new \Requests_Hooks(); |
|
| 286 | + $hooks->register('curl.before_send', function ($fp) use ($data) { |
|
| 287 | + curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); |
|
| 288 | + curl_setopt($fp, CURLOPT_POSTFIELDS, $data); |
|
| 289 | + }); |
|
| 290 | + $options = ['hooks' => $hooks]; |
|
| 291 | + // no need to set the body, it's taken care of by hooks |
|
| 292 | + $data = []; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + return self::request($url, $headers, $data, self::POST, $options); |
|
| 296 | + } |
|
| 297 | 297 | /** |
| 298 | 298 | * Send a PUT request |
| 299 | 299 | */ |
@@ -1005,15 +1005,15 @@ discard block |
||
| 1005 | 1005 | return false; |
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | - protected static function httpBuildQuery(array &$data, $key = '', $value = null) { |
|
| 1009 | - foreach ($value ?? $data as $k => $v) { |
|
| 1010 | - if (is_array($v)) { |
|
| 1011 | - self::httpBuildQuery($data, "{$key}[{$k}]", $v); |
|
| 1012 | - }else{ |
|
| 1013 | - $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; |
|
| 1014 | - $data[$cur_key] = $v; |
|
| 1015 | - } |
|
| 1016 | - unset($data[$k]); |
|
| 1017 | - } |
|
| 1018 | - } |
|
| 1008 | + protected static function httpBuildQuery(array &$data, $key = '', $value = null) { |
|
| 1009 | + foreach ($value ?? $data as $k => $v) { |
|
| 1010 | + if (is_array($v)) { |
|
| 1011 | + self::httpBuildQuery($data, "{$key}[{$k}]", $v); |
|
| 1012 | + }else{ |
|
| 1013 | + $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; |
|
| 1014 | + $data[$cur_key] = $v; |
|
| 1015 | + } |
|
| 1016 | + unset($data[$k]); |
|
| 1017 | + } |
|
| 1018 | + } |
|
| 1019 | 1019 | } |
@@ -269,13 +269,13 @@ discard block |
||
| 269 | 269 | if (count($files)) { |
| 270 | 270 | |
| 271 | 271 | // replacing file paths with curlFile Object |
| 272 | - array_walk($files, function ($filePath, $key) use (&$data) { |
|
| 272 | + array_walk($files, function($filePath, $key) use (&$data) { |
|
| 273 | 273 | if (is_array($filePath)) { |
| 274 | - array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { |
|
| 274 | + array_walk($filePath, function($subFilePath, $subKey) use (&$data, $key) { |
|
| 275 | 275 | $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); |
| 276 | 276 | }); |
| 277 | 277 | } else { |
| 278 | - $data[ $key ] = new \CURLFile(realpath($filePath)); |
|
| 278 | + $data[$key] = new \CURLFile(realpath($filePath)); |
|
| 279 | 279 | } |
| 280 | 280 | }); |
| 281 | 281 | |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | |
| 284 | 284 | // starting to add a hook to attach file to request |
| 285 | 285 | $hooks = new \Requests_Hooks(); |
| 286 | - $hooks->register('curl.before_send', function ($fp) use ($data) { |
|
| 286 | + $hooks->register('curl.before_send', function($fp) use ($data) { |
|
| 287 | 287 | curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true); |
| 288 | 288 | curl_setopt($fp, CURLOPT_POSTFIELDS, $data); |
| 289 | 289 | }); |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | * @return string Default certificate path. |
| 561 | 561 | */ |
| 562 | 562 | public static function get_certificate_path() { |
| 563 | - if ( ! empty( Requests::$certificate_path ) ) { |
|
| 563 | + if (!empty(Requests::$certificate_path)) { |
|
| 564 | 564 | return Requests::$certificate_path; |
| 565 | 565 | } |
| 566 | 566 | |
@@ -572,7 +572,7 @@ discard block |
||
| 572 | 572 | * |
| 573 | 573 | * @param string $path Certificate path, pointing to a PEM file. |
| 574 | 574 | */ |
| 575 | - public static function set_certificate_path( $path ) { |
|
| 575 | + public static function set_certificate_path($path) { |
|
| 576 | 576 | Requests::$certificate_path = $path; |
| 577 | 577 | } |
| 578 | 578 | |
@@ -1009,7 +1009,7 @@ discard block |
||
| 1009 | 1009 | foreach ($value ?? $data as $k => $v) { |
| 1010 | 1010 | if (is_array($v)) { |
| 1011 | 1011 | self::httpBuildQuery($data, "{$key}[{$k}]", $v); |
| 1012 | - }else{ |
|
| 1012 | + } else { |
|
| 1013 | 1013 | $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; |
| 1014 | 1014 | $data[$cur_key] = $v; |
| 1015 | 1015 | } |
@@ -274,7 +274,8 @@ discard block |
||
| 274 | 274 | array_walk($filePath, function ($subFilePath, $subKey) use (&$data, $key) { |
| 275 | 275 | $data[$key][$subKey] = new \CURLFile(realpath($subFilePath)); |
| 276 | 276 | }); |
| 277 | - } else { |
|
| 277 | + } |
|
| 278 | + else { |
|
| 278 | 279 | $data[ $key ] = new \CURLFile(realpath($filePath)); |
| 279 | 280 | } |
| 280 | 281 | }); |
@@ -1009,7 +1010,8 @@ discard block |
||
| 1009 | 1010 | foreach ($value ?? $data as $k => $v) { |
| 1010 | 1011 | if (is_array($v)) { |
| 1011 | 1012 | self::httpBuildQuery($data, "{$key}[{$k}]", $v); |
| 1012 | - }else{ |
|
| 1013 | + } |
|
| 1014 | + else { |
|
| 1013 | 1015 | $cur_key = $key ? "{$key}[{$k}]" : "{$k}"; |
| 1014 | 1016 | $data[$cur_key] = $v; |
| 1015 | 1017 | } |