Using Guzzle for uploading files to a REST API:
My PHP Code is:
use GuzzleHttpClient;
use GuzzleHttpRequestOptions;
use GuzzleHttpPsr7;
$apiClient = new Client([
'base_uri' => $url,
'timeout' => 5, // seconds
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
]);
$postParams = [
RequestOptions::MULTIPART => [
...$multipartParams,
[
'name' => 'upload',
'contents' => Psr7Utils::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): GuzzleHttpHandlerCurlFactory::createRejection(Object(GuzzleHttpHandlerEasyHandle), Array)
#1 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(110): GuzzleHttpHandlerCurlFactory::finishError(Object(GuzzleHttpHandlerCurlHandler), Object(GuzzleHttpHandlerEasyHandle), Object(GuzzleHttpHandlerCurlFactory))
#2 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(47): GuzzleHttpHandlerCurlFactory::finish(Object(GuzzleHttpHandlerCurlHandler), Object(GuzzleHttpHandlerEasyHandle), Object(GuzzleHttpHandlerCurlFactory))
#3 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(28): GuzzleHttpHandlerCurlHandler->__invoke(Object(GuzzleHttpPsr7Request), Array)
#4 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(48): GuzzleHttpHandlerProxy::GuzzleHttpHandler{closure}(Object(GuzzleHttpPsr7Request), Array)
#5 /app/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(64): GuzzleHttpHandlerProxy::GuzzleHttpHandler{closure}(Object(GuzzleHttpPsr7Request), Array)
#6 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttpPrepareBodyMiddleware->__invoke(Object(GuzzleHttpPsr7Request), Array)
#7 /app/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Request), Array)
#8 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(63): GuzzleHttpRedirectMiddleware->__invoke(Object(GuzzleHttpPsr7Request), Array)
#9 /app/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Request), Array)
#10 /app/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttpHandlerStack->__invoke(Object(GuzzleHttpPsr7Request), Array)
#11 /app/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttpClient->transfer(Object(GuzzleHttpPsr7Request), Array)
#12 /app/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttpClient->requestAsync('POST', Object(GuzzleHttpPsr7Uri), Array)
#13 /app/vendor/guzzlehttp/guzzle/src/ClientTrait.php(95): GuzzleHttpClient->request('POST', 'upload', Array)
#14 /app/CkanApiClient.php(140): GuzzleHttpClient->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
2
Answers
SOLVED
The problem was the timeout of 5 seconds. Large files take longer to upload.
Setting the timeout to 30 seconds was the solution:
I think you php configurations are blocking upload.
guzzlehttp
doesn’t have one.Note:
curl
command in cli doesn’t have limitationsYou need to increase the
upload_max_filesize
andpost_max_size
inphp.ini
:Then restart your
php-fpm
or any program that runs your php.You can’t change these valuesat runtime; uploads of file larger than the value specified in php.ini will have failed.
Read more about php.ini directives