PHP:发送大文件时 Guzzle 失败

问题描述 投票:0回答:1

使用 Guzzle 将文件上传到 REST API:

我的 PHP 代码是:

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);

对于超过 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}

在命令行上使用

curl
执行文件上传时,上传工作。

如何让我的代码工作?如何调试错误是什么?

我的环境:

  • 作曲家:guzzlehttp/guzzle 7.5.0
  • Docker文件:
    php:8.2-cli
  • PHP:
    memory_limit = 2048M
php guzzle php-curl
1个回答
0
投票

我认为你的 php 配置阻止了上传。

guzzlehttp
没有。

注意:

curl
cli 中的命令没有限制

你需要增加

upload_max_filesize
post_max_size
php.ini

upload_max_filesize = 100M
post_max_size = 100M

然后重新启动您的

php-fpm
或任何运行您的php的程序。

你不能在运行时改变这些值;上传大于 php.ini 中指定值的文件将失败。

阅读更多关于 php.ini 指令的信息

© www.soinside.com 2019 - 2024. All rights reserved.