Search code examples
phpguzzlephp-curl

PHP: Guzzle fails when sending large files


Using Guzzle for uploading files to a REST API:

My PHP Code is:

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Psr7;

$apiClient = new Client([
    'base_uri' => $url,
    'timeout'  => 5, // seconds
    'headers' => [
        'Content-Type' => 'application/json',
        'Accept'     => 'application/json',
    ],
]);

$postParams = [
    RequestOptions::MULTIPART => [
        ...$multipartParams,
        [                        
            'name' => 'upload',
            'contents' => Psr7\Utils::streamFor(fopen($filePath, 'r')),
            'filename' => $postFields['filename'],
        ]
    ]
];

$response = $apiClient->post("upload", $postParams);

This code results in errors for files above 10MB:

Error: #0 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(158): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)
#1 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(110): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#2 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(47): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#3 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(48): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#5 /app/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(64): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#6 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#7 /app/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#8 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(63): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#9 /app/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array)
#10 /app/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#11 /app/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array)
#12 /app/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync('POST', Object(GuzzleHttp\Psr7\Uri), Array)
#13 /app/vendor/guzzlehttp/guzzle/src/ClientTrait.php(95): GuzzleHttp\Client->request('POST', 'upload', Array)
#14 /app/CkanApiClient.php(140): GuzzleHttp\Client->post('upload', Array)
#15 /app/ckan-upload.php(142): CkanApiClient->send('upload', Array)
#16 {main}

When performing file uploads with curl on the command line the uploads work.

How to make my code work? How to debug what's the error?

My environment:

  • Composer: guzzlehttp/guzzle 7.5.0
  • Dockerfile: php:8.2-cli
  • PHP: memory_limit = 2048M

Solution

  • SOLVED

    The problem was the timeout of 5 seconds. Large files take longer to upload.

    Setting the timeout to 30 seconds was the solution:

    $apiClient = new Client([
        'base_uri' => $url,
        'timeout'  => 30, // seconds
        'headers' => [
            'Content-Type' => 'application/json',
            'Accept'     => 'application/json',
        ],
    ]);