我在PHP中有一个gcloud函数,它接收一个json对象。在我的函数中,我读取json对象如下: file_get_contents('php://input')。
如果我在函数中接收的json对象小于16 it,但当json对象大于16 it时,则返回空。
发布于 2022-02-07 06:28:55
没有代码,客户端也没有服务器端.
您可能会以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);
}最近,基于文档将一个模板编写器项目推到了GitHub。对于$body->rewind()并不完全确定,这只是流所必需的(它可能已经返回了string )。这需要"guzzlehttp/psr7": "^2.1.0"。另见:什么是PSR-7和如何使用它?
https://stackoverflow.com/questions/71013872
复制相似问题