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

WBOY
WBOY 转载
2023-09-11 14:16:01 880浏览

To get the details of the HTTP request you need to make use of the class Illuminate\Http\Request.

使用上面的类,您将能够从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 −

Illuminate\Support\Collection 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

以上就是在Laravel中如何获取HTTP请求体的内容?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除