You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the bundled Http Client, when passing a custom option using the withOptions method, if a default value is already set for that option, they will be placed into an array containing both values instead of overwriting the default value with the provided one. This behavior is due to the use of array_merge_recursive, which merges string keys with the same name into an array, as documented.
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too.
Since Laravel 9, a default connect_timeout value is set, so specifiying a custom connect_timeout with withOptions results in Laravel passing an array of timeouts to Guzzle instead of passing our custom connect_timeout. This problems is already present in 8.x versions but since only the http_errors options had a default value, it may have gone unnoticed.
Route::get('/', function () {
returnHttp::withOptions([
'connect_timeout' => 10
])->get('https://example.com');
});
Browse to the / route to see error.
Possible solution
Using array_replace_recursive instead of array_merge_recursive seems to do the job.
If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is.
Description:
Using the bundled
Http
Client, when passing a custom option using thewithOptions
method, if a default value is already set for that option, they will be placed into an array containing both values instead of overwriting the default value with the provided one. This behavior is due to the use ofarray_merge_recursive
, which merges string keys with the same name into an array, as documented.https://www.php.net/manual/en/function.array-merge-recursive.php
Since Laravel 9, a default
connect_timeout
value is set, so specifiying a customconnect_timeout
withwithOptions
results in Laravel passing an array of timeouts to Guzzle instead of passing our customconnect_timeout
. This problems is already present in 8.x versions but since only thehttp_errors
options had a default value, it may have gone unnoticed.Steps To Reproduce:
Repository to demonstrate the issue: https://github.com/nhedger/bug-report-http-client
I've placed the code in the default route.
Browse to the
/
route to see error.Possible solution
Using
array_replace_recursive
instead ofarray_merge_recursive
seems to do the job.https://www.php.net/array_replace_recursive
The text was updated successfully, but these errors were encountered: