【问题标题】:gcloud function php post size issuegcloud函数php帖子大小问题
【发布时间】:2022-09-27 21:55:20
【问题描述】:

我在 PHP 中有一个接收 json 对象的 gcloud 函数。 在我的函数中,我像这样读取 json 对象:file_get_contents(\'php://input\')。

当我在函数中收到的 json 对象小于 16KB 时,这对我有用,但是当 json 对象大于 16KB 时,它返回空。

  • 这是因为 PHP 函数接收到的有效负载超过 16kb。如果数据小于 16kb,则没有问题。尝试在发送前应用压缩(如 gzip)使数据“更小”,然后在接收端,可以将其分解以使其“更大”。在这种情况下,压缩和解压缩完全由应用程序代码而不是基础架构处理。

标签: php function google-cloud-functions


【解决方案1】:

没有代码,无论是客户端还是服务器端......

您可能会发送为enctype="multipart/form-data" ? https://php.net/manual/en/wrappers.php.php#wrappers.php.input
file_get_contents('php://input') 本身是有问题的。

而是使用默认方法签名,它提供ServerRequestInterface $request

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
...

public static function on_https( ServerRequestInterface $request ): ResponseInterface {

    $params = $request->getQueryParams();
    $body = $request->getBody();

    $body->rewind(); // or $body->seek(0);
    $bodyText = $body->getContents();
    $data = json_decode($bodyText);

    return (new Response())
        ->withHeader('Content-Type', 'application/json; charset=utf-8')
        ->withBody(Utils::streamFor(json_encode($data)))
        ->withStatus(200);
}

最近刚刚推送了一个模板编写器project 到 GitHub,基于文档。不完全确定$body->rewind(),这只是流所必需的(它可能已经返回string)。这需要"guzzlehttp/psr7": "^2.1.0"。另见:What is PSR-7 and how to use it?

【讨论】:

  • 这在我的 gcloud 函数代码中 <?php print file_get_contents('php://input'); ?> 我以原始格式发送内容
  • 我怎么能调用那个函数?
  • 我在本地使用 file_get_contents('php://input') 并且效果很好,但在 gcloud 函数中仅在内容小于 16KB 时才有效
【解决方案2】:

我在使用带有 php 7.4 的 Cloud Function gen1 时遇到了同样的问题,但我找不到任何方法让它工作。

我用 php8.1 和 Cloud Function gen2 进行了测试其作品.

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多