Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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<string, scalar>`
to `array<string, string>`: non-string values are passed already JSON-encoded.

## 0.19 April 18, 2026

- New #195: Add `InputFile::filename()` and `InputFile::extension()` methods.
Expand Down
2 changes: 1 addition & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
5 changes: 2 additions & 3 deletions src/Transport/NativeTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +122,7 @@ static function (int $errorNumber, string $errorString): bool {
}

/**
* @psalm-param array<string, mixed> $data
* @psalm-param array<string, string> $data
* @psalm-param array<string, InputFile> $files
*/
private function buildMultipartFormData(array $data, array $files, string $boundary): string
Expand All @@ -134,7 +133,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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Transport/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, scalar> $data
* @psalm-param array<string, string> $data
* @psalm-param array<string, InputFile> $files
*/
public function postWithFiles(string $url, array $data, array $files): ApiResponse;
Expand Down
2 changes: 1 addition & 1 deletion tests/Transport/NativeTransport/NativeTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
Loading