使用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);
对于大于10 MB的文件,此代码会导致错误:
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
执行文件上载时,上载工作正常。
如何使我的代码工作?如何调试错误?
我的环境:
- composer :狂饮http/狂饮7.5.0
- 停靠文件:
php:8.2-cli
- PHP:数据库扩展
2条答案
按热度按时间g2ieeal71#
我认为你的php配置阻止了上传。
guzzlehttp
没有。注意:cli中的
curl
命令没有限制您需要在
php.ini
中增加upload_max_filesize
和post_max_size
:然后重新启动你的
php-fpm
或者任何运行php的程序。您不能在运行时更改这些值;上传大于php.ini中指定值的文件将失败。
Read more about php.ini directives
ivqmmu1c2#
已解决
问题是5秒的超时。大文件需要更长的时间才能上传。
将超时设置为30秒的解决方案是: