PHP:Laravel获取提交的原始数据
测试环境
composer.json
{"require": {"php": "^8.0.2","laravel/framework": "^9.19",}
}
获取方式
获取接口提交的原始数据
// 使用5.6之前的PHP,则php://input流只能读取一次
$body = file_get_contents('php://input');// 或者
$body = $request->getContent()
备注:在PhpStrom中会提示,忽略即可,代码是可以正常运行的
Method 'getContent' not found in \\Illuminate\\Http\\Request
代码示例
public function getContent(Request $request): array{$body = $request->getContent();$input = $request->input();Log::debug($body);Log::debug($input);return ['body' => $body,'input' => $input];}
提交数据
POST {{api_url}}/test/getContent
Accept: application/json
Content-Type: application/json; charset=utf-8{"name": "post_name"
}
数据返回
{"body": "{\\n \\"name\\": \\"post_name\\"\\n}","input": {"name": "post_name"}}
参考文章
- 如何在laravel中获取原始表单数据?