Fix Guzzle InvalidArgumentException for POST with array parameters

This commit is contained in:
Philipp Holzer 2022-11-09 22:17:31 +01:00
parent 75360f3b8c
commit 82c631eae3
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
3 changed files with 16 additions and 3 deletions

View file

@ -92,7 +92,7 @@ interface ICanSendHttpRequests
* Send POST request to an URL * Send POST request to an URL
* *
* @param string $url URL to post * @param string $url URL to post
* @param mixed $params array of POST variables * @param mixed $params POST variables (if an array is passed, it will automatically set as formular parameters)
* @param array $headers HTTP headers * @param array $headers HTTP headers
* @param int $timeout The timeout in seconds, default system config value or 60 seconds * @param int $timeout The timeout in seconds, default system config value or 60 seconds
* *
@ -107,6 +107,7 @@ interface ICanSendHttpRequests
* @param string $url Url to send to * @param string $url Url to send to
* @param array $opts (optional parameters) associative array with: * @param array $opts (optional parameters) associative array with:
* 'body' => (mixed) setting the body for sending data * 'body' => (mixed) setting the body for sending data
* 'form_params' => (array) Associative array of form field names to values
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file

View file

@ -140,6 +140,10 @@ class HttpClient implements ICanSendHttpRequests
$conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY]; $conf[RequestOptions::BODY] = $opts[HttpClientOptions::BODY];
} }
if (!empty($opts[HttpClientOptions::FORM_PARAMS])) {
$conf[RequestOptions::FORM_PARAMS] = $opts[HttpClientOptions::FORM_PARAMS];
}
if (!empty($opts[HttpClientOptions::AUTH])) { if (!empty($opts[HttpClientOptions::AUTH])) {
$conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH]; $conf[RequestOptions::AUTH] = $opts[HttpClientOptions::AUTH];
} }
@ -205,7 +209,11 @@ class HttpClient implements ICanSendHttpRequests
{ {
$opts = []; $opts = [];
$opts[HttpClientOptions::BODY] = $params; if (!is_array($params)) {
$opts[HttpClientOptions::BODY] = $params;
} else {
$opts[HttpClientOptions::FORM_PARAMS] = $params;
}
if (!empty($headers)) { if (!empty($headers)) {
$opts[HttpClientOptions::HEADERS] = $headers; $opts[HttpClientOptions::HEADERS] = $headers;

View file

@ -59,9 +59,13 @@ class HttpClientOptions
const VERIFY = 'verify'; const VERIFY = 'verify';
/** /**
* body: (mixed) Setting the body for sending data * body: (string) Setting the body for sending data
*/ */
const BODY = RequestOptions::BODY; const BODY = RequestOptions::BODY;
/**
* form_params: (array) Associative array of form field names to values
*/
const FORM_PARAMS = RequestOptions::FORM_PARAMS;
/** /**
* auth: (array) Authentication settings for specific requests * auth: (array) Authentication settings for specific requests
*/ */