首頁  >  文章  >  後端開發  >  在Laravel中如何取得HTTP請求體的內容?

在Laravel中如何取得HTTP請求體的內容?

WBOY
WBOY轉載
2023-09-11 13:49:011391瀏覽

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.com。如有侵權,請聯絡admin@php.cn刪除