卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

在Laravel中如何获取HTTP请求体的内容?

To get the details of the HTTP request you need to make use of the class IlluminateHttpRequest.

使用上面的类,您将能够从HTTP请求中获取输入、cookies和文件。现在考虑以下表单 -

在Laravel中如何获取HTTP请求体的内容?

To get all the details from the HTTP request you can do as follows −

Example 1

的翻译为:

示例 1

Using $request->all() method

在下面的表单中输入以下详细信息:

在Laravel中如何获取HTTP请求体的内容?

Once you submit it will retrieve all the input data and return an array with data.

'
public function validateform(Request $request) {
   $input = $request->all();
   print_r($input);
}

输出

The output of the above code is −

'
Array (
   [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx
   [name] => Rasika Desai
   [email] => rasika@gmail.com
   [age] => 20
   [address] => Pune
)

Example 2

的中文翻译为:

示例2

Using $request->collect() method.

This method will return the data as a collection.

'
public function validateform(Request $request) {
   $input = $request->collect();
   print_r($input);
}

输出

The output of the above code is −

'
IlluminateSupportCollection Object (
   [items:protected] => Array(
      [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx
      [name] => Rasika Desai
      [email] => rasika@gmail.com
      [age] => 20
      [address] => Pune
   )
   [escapeWhenCastingToString:protected] =>
)

Example 3

使用 $request->getContent() 方法。

此方法将输出为URL查询字符串,数据以键/值对的形式传递。

'
public function validateform(Request $request) {
   $input = $request->getContent();
   echo $input;
}

输出

The output of the above code is

'
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune

Example 4

使用 php://input

这将返回来自URL查询字符串中输入字段的数据。

'
$data = file_get_contents('php://input');
print_r($data);

输出

The output of the above code is −

'
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
卓越飞翔博客
上一篇: C# 中的类型转换是什么?
下一篇: PHP 登录鉴权框架简介及开发实践
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏