phpswooleoctane

file_get_contents('php://input') not working on Laravel+Octane / Swoole


I'm migrating from Laravel 8 to Laravel 8 + Octane / Swoole. All works fine, but php://input always are empty. Also, I check $_POST and $_SERVER values.

file_get_contents('php://input') is used by AWS SNS Message Validator.

Any alterantive to read php://input?

PHP code

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

With PHP-FPM

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

With Octane+Swoole

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

Solution

  • Problem

    php://input are not available on Swoole. Always are the same running process.

    Solution: PSR-7 Request

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

    Of course, with octane, symfony/psr-http-message-bridge and nyholm/psr7 are required for Laravel PSR-7 requests.

    Also, if your problem is related with AWS SES, you need to change Message::fromRawPostData() to Message::fromPsrRequest($request).