【问题标题】:file_get_contents('php://input') not working on Laravel+Octane / Swoolefile_get_contents('php://input') 在 Laravel+Octane / Swoole 上不起作用
【发布时间】:2025-12-16 19:10:01
【问题描述】:

我正在从 Laravel 8 迁移到 Laravel 8 + Octane / Swoole。一切正常,但php://input 总是空的。另外,我检查了 $_POST 和 $_SERVER 的值。

file_get_contents('php://input')AWS SNS Message Validator 使用。

阅读php://input的任何替代方法?

PHP 代码

echo "php://input: ".file_get_contents('php://input');

使用 PHP-FPM

$ curl -i -X POST --data "dataaaa" https://example.com/aws/sns/webhook
php://input: dataaaa

使用辛烷值+Swoole

$ curl -i -X POST --data "dataaaa" https://example.com/aws/sns/webhook
php://input:

【问题讨论】:

  • php://input 是 PHP 内置的,那些框架不应该改变它。
  • 我想的和你一样,但是用swoole总是空的。使用 PHP-FPM 可以正常工作。
  • 请注意,在 CLI SAPI 或内置网络服务器中,php://input 流包装器不适用于 enctype="multipart/form-data"。见the manual for details

标签: php swoole octane


【解决方案1】:

问题

php://input 在 Swoole 上不可用。总是相同的运行进程。

解决方案:PSR-7 请求

use Psr\Http\Message\RequestInterface;

public function sesSubscriptionWebhook(RequestInterface $request)
{
    // $input = file_get_contents('php://input'); // dont work on swoole
    $input = $request->getBody();
}

当然,使用辛烷值时,Laravel PSR-7 requests 需要 symfony/psr-http-message-bridgenyholm/psr7

另外,如果您的问题与 AWS SES 有关,您需要将Message::fromRawPostData() 更改为Message::fromPsrRequest($request)

【讨论】: