Skip to content

Commit d44a9d5

Browse files
authored
Merge pull request #454 from BoShurik/http-client-symfony
Better file upload support in symfony/http-client
2 parents 452aee1 + ee6311e commit d44a9d5

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161

6262
-
6363
name: Remove http client dependencies
64-
run: composer remove psr/http-client psr/http-factory php-http/multipart-stream-builder symfony/http-client guzzlehttp/guzzle --dev --no-update
64+
run: composer remove psr/http-client psr/http-factory php-http/multipart-stream-builder symfony/http-client symfony/mime guzzlehttp/guzzle --dev --no-update
6565

6666
-
6767
name: Install dependencies with composer

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@
3030
"psr/http-factory": "^1.0",
3131
"php-http/multipart-stream-builder": "^1.0",
3232
"symfony/http-client": "^4.3 | ^5.0 | ^6.0",
33+
"symfony/mime": "^4.3 | ^5.0 | ^6.0",
3334
"guzzlehttp/guzzle": "^7.0"
3435
},
3536
"suggest": {
3637
"psr/http-client": "To use psr/http-client",
3738
"psr/http-factory": "To use psr/http-client",
3839
"php-http/multipart-stream-builder": "To use psr/http-client",
39-
"guzzlehttp/guzzle": "To use psr/http-client",
40-
"symfony/http-client": "To use symfony/http-client"
40+
"guzzlehttp/guzzle": "To use guzzlehttp/guzzle psr implementation",
41+
"symfony/http-client": "To use symfony/http-client",
42+
"symfony/mime": "To use symfony/http-client"
4143
},
4244
"autoload": {
4345
"psr-4": {

src/Http/SymfonyHttpClient.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TelegramBot\Api\Http;
44

5+
use Symfony\Component\Mime\Part\DataPart;
6+
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
57
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
68
use Symfony\Contracts\HttpClient\HttpClientInterface as SymfonyHttpClientInterface;
79
use TelegramBot\Api\HttpException;
@@ -26,13 +28,19 @@ protected function doRequest($url, array $data = null)
2628
$options = [];
2729
if ($data) {
2830
$method = 'POST';
31+
32+
$data = array_filter($data);
2933
foreach ($data as &$value) {
3034
if ($value instanceof \CURLFile) {
31-
$value = fopen($value->getFilename(), 'r');
35+
$value = DataPart::fromPath($value->getFilename());
36+
} elseif (!\is_string($value) && !\is_array($value)) {
37+
$value = (string) $value;
3238
}
3339
}
40+
$formData = new FormDataPart($data);
3441

35-
$options['body'] = $data;
42+
$options['headers'] = $formData->getPreparedHeaders()->toArray();
43+
$options['body'] = $formData->toIterable();
3644
} else {
3745
$method = 'GET';
3846
}

0 commit comments

Comments
 (0)