How do I get raw form data in laravel?

Benubird picture Benubird · Feb 24, 2014 · Viewed 16.4k times · Source

I have a script that is trying to send data to my site using HTTP PUT. Normally, I woudl just retrieve it by reading from the input stream with file_get_contents('php://input'). However, when I try that with laravel, I don't get anything! Why not? How do I read the raw input data?

Answer

Benubird picture Benubird · Feb 24, 2014

Laravel intercepts all input. If you're using PHP prior to 5.6, the php://input stream can only be read once. This means that you need to get the data from the framework. You can do this by accessing the getContent method on the Request instance, like this:

$content = Request::getContent(); // Using Request facade
     /* or */ 
$content = $request->getContent(); // If you already have a Request instance
                                   // lying around, from say the controller  

Since Illuminate\Request extends Symfony\Component\HttpFoundation\Request, and getContent is defined here: http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/Request.html#method_getContent