From dcc2a67c0ef19f0e82ac6648a062d896c6315191 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 23 Apr 2026 15:11:00 +0300 Subject: [PATCH 1/2] Pass JSON-encoded strings to `TransportInterface::postWithFiles()` instead of scalars --- CHANGELOG.md | 5 +++++ src/Api.php | 2 +- src/Transport/NativeTransport.php | 4 ++-- src/Transport/TransportInterface.php | 5 +++-- tests/Transport/NativeTransport/NativeTransportTest.php | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 110f8af2..651f0a54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Telegram Bot API for PHP Change Log +## 0.20 April 23, 2026 + +- Chg #197: `$data` parameter type in `TransportInterface::postWithFiles()` changed from `array` + to `array`: non-string values are passed already JSON-encoded. + ## 0.19 April 18, 2026 - New #195: Add `InputFile::filename()` and `InputFile::extension()` methods. diff --git a/src/Api.php b/src/Api.php index b2d7d715..855839f0 100644 --- a/src/Api.php +++ b/src/Api.php @@ -153,7 +153,7 @@ private function sendPostRequest(string $url, array $data): ApiResponse } $data = array_map( - static fn(mixed $value) => is_scalar($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), + static fn(mixed $value) => is_string($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR), $data, ); diff --git a/src/Transport/NativeTransport.php b/src/Transport/NativeTransport.php index 9955ca38..bc907503 100644 --- a/src/Transport/NativeTransport.php +++ b/src/Transport/NativeTransport.php @@ -123,7 +123,7 @@ static function (int $errorNumber, string $errorString): bool { } /** - * @psalm-param array $data + * @psalm-param array $data * @psalm-param array $files */ private function buildMultipartFormData(array $data, array $files, string $boundary): string @@ -134,7 +134,7 @@ private function buildMultipartFormData(array $data, array $files, string $bound $result[] = "--$boundary"; $result[] = "Content-Disposition: form-data; name=\"$key\""; $result[] = ''; - $result[] = is_string($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR); + $result[] = $value; } foreach ($files as $key => $file) { diff --git a/src/Transport/TransportInterface.php b/src/Transport/TransportInterface.php index 3da3d053..dd606103 100644 --- a/src/Transport/TransportInterface.php +++ b/src/Transport/TransportInterface.php @@ -41,12 +41,13 @@ public function post(string $url, string $body, array $headers): ApiResponse; * Performs a POST request with file uploads. * * @param string $url The URL to request. - * @param (bool|string|int|float)[] $data The form data fields as an associative array (field name => field value). + * @param string[] $data The form data fields as an associative array (field name => field value). All values are + * strings: plain strings are passed as-is, non-string values (integers, booleans, arrays) are JSON-encoded. * @param InputFile[] $files The files to upload as an associative array (field name => `InputFile`). * * @return ApiResponse The API response. * - * @psalm-param array $data + * @psalm-param array $data * @psalm-param array $files */ public function postWithFiles(string $url, array $data, array $files): ApiResponse; diff --git a/tests/Transport/NativeTransport/NativeTransportTest.php b/tests/Transport/NativeTransport/NativeTransportTest.php index 83cab72c..e365b9af 100644 --- a/tests/Transport/NativeTransport/NativeTransportTest.php +++ b/tests/Transport/NativeTransport/NativeTransportTest.php @@ -157,7 +157,7 @@ public function testPostWithFiles(): void $transport->postWithFiles( 'http://url/method', [ - 'ages' => [23, 45], + 'ages' => '[23,45]', ], [ 'file1' => new InputFile(__DIR__ . '/test.txt'), From bc08a5bebc237a60a0193cafb8a29bc1f3cc83ad Mon Sep 17 00:00:00 2001 From: vjik <525501+vjik@users.noreply.github.com> Date: Thu, 23 Apr 2026 12:12:04 +0000 Subject: [PATCH 2/2] Apply PHP CS Fixer and Rector changes (CI) --- src/Transport/NativeTransport.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Transport/NativeTransport.php b/src/Transport/NativeTransport.php index bc907503..08eb09ae 100644 --- a/src/Transport/NativeTransport.php +++ b/src/Transport/NativeTransport.php @@ -11,7 +11,6 @@ use function file_get_contents; use function is_string; -use function json_encode; /** * @see https://www.php.net/manual/function.file-get-contents.php