如何使用 PHP 接收 JSON POST 請求


要使用 PHP 接收 JSON POST 請求,您可以按照以下步驟操作

  • 確保傳送到 PHP 指令碼的請求格式為 JSON 物件。

  • 在您的 PHP 指令碼中,使用 `file_get_contents('php://input')` 函式檢索原始 POST 資料。此函式讀取 HTTP 請求的原始輸入流。

  • 使用 `json_decode()` 函式將接收到的 JSON 資料解碼為 PHP 關聯陣列或物件。

  • 然後,您可以訪問解碼後的資料並對其執行任何必要的操作或處理。

有多種方法可以在 PHP 中接收 JSON POST 請求。以下是三種常見方法

  • 使用 `file_get_contents('php://input')`

  • 使用 `$_POST` 超全域性變數

  • 使用 `json_decode()` 與 `$_REQUEST`

使用 `file_get_contents('php://input')`

要使用 PHP 中的 `file_get_contents('php://input')` 方法接收 JSON POST 請求,請按照以下步驟操作

在請求正文中傳送 JSON 資料,並將 `Content-Type` 標頭設定為 `application/json`。

在您的 PHP 指令碼中,使用 `file_get_contents('php://input')` 函式檢索原始 POST 資料。

使用 `json_decode()` 函式將接收到的 JSON 資料解碼為 PHP 關聯陣列或物件。

然後,您可以訪問解碼後的資料並對其執行任何必要的操作或處理。

示例

以下是一個程式碼片段示例,演示瞭如何使用 `file_get_contents('php://input')` 接收和處理 JSON POST 請求

<?php
// Retrieve the raw POST data
$jsonData = file_get_contents('php://input');
// Decode the JSON data into a PHP associative array
$data = json_decode($jsonData, true);
// Check if decoding was successful
if ($data !== null) {
   // Access the data and perform operations
   $name = $data['name'];
   $age = $data['age'];
   // Perform further processing or respond to the request
} else {
   // JSON decoding failed
   http_response_code(400); // Bad Request
   echo "Invalid JSON data";
}
?>

在此示例中,使用 `file_get_contents('php://input')` 檢索 JSON POST 資料並將其儲存在 `$jsonData` 變數中。然後,使用 `json_decode()` 函式將 JSON 資料解碼為 PHP 關聯陣列,並將其儲存在 `$data` 變數中。

您可以使用相應的陣列鍵(例如,`$data['name']`、`$data['age']`)訪問接收到的資料,並根據您的特定需求執行任何必要的操作或處理。

請記住處理錯誤情況,例如 JSON 解碼由於 JSON 無效而失敗的情況。在上面的示例中,提供了適當的 HTTP 響應程式碼(400 錯誤請求)和錯誤訊息來處理這種情況。

使用 `$_POST` 超全域性變數

要使用 PHP 中的 `$_POST` 超全域性變數接收 JSON POST 請求,請按照以下步驟操作

在請求正文中傳送 JSON 資料,並將 `Content-Type` 標頭設定為 `application/json`。

在您的 PHP 指令碼中,從 `$_POST` 超全域性變數訪問 JSON 資料。

JSON 資料將自動解析,並作為關聯陣列在 `$_POST` 中可用。

然後,您可以訪問接收到的資料並對其執行任何必要的操作或處理。

示例

以下是一個程式碼片段示例,演示瞭如何使用 `$_POST` 超全域性變數接收和處理 JSON POST 請求

<?php
// Access the JSON data from $_POST
$data = $_POST;
// Check if data is available
if (!empty($data)) {
   // Access the data and perform operations
   $name = $data['name'];
   $age = $data['age'];
   // Perform further processing or respond to the request
} else {
   // No data received
   http_response_code(400); // Bad Request
   echo "No JSON data received";
}
?>

在此示例中,JSON POST 資料會自動解析,並在 `$_POST` 超全域性變數中可用。接收到的資料儲存在 `$data` 變數中,可以將其作為關聯陣列訪問。

您可以使用相應的陣列鍵(例如,`$data['name']`、`$data['age']`)訪問接收到的資料,並根據您的特定需求執行任何必要的操作或處理。

如果沒有接收到資料或請求不包含有效的 JSON,您可以相應地處理錯誤情況。在上面的示例中,提供了適當的 HTTP 響應程式碼(400 錯誤請求)和錯誤訊息來處理未接收到 JSON 資料的情況。

使用 `json_decode()` 與 `$_REQUEST`

要使用 PHP 中的 `json_decode()` 函式與 `$_REQUEST` 接收 JSON POST 請求,請按照以下步驟操作

在請求正文中傳送 JSON 資料,並將 `Content-Type` 標頭設定為 `application/json`。

在您的 PHP 指令碼中,使用 `file_get_contents('php://input')` 函式檢索原始 POST 資料。

使用 `json_decode()` 函式將接收到的 JSON 資料解碼為 PHP 關聯陣列或物件。

將解碼後的資料分配給 `$_REQUEST` 超全域性變數。

示例

以下是一個程式碼片段示例,演示瞭如何使用 `json_decode()` 與 `$_REQUEST` 接收和處理 JSON POST 請求

<?php
// Retrieve the raw POST data
$jsonData = file_get_contents('php://input');
// Decode the JSON data into a PHP associative array
$data = json_decode($jsonData, true);
// Assign the decoded data to $_REQUEST
$_REQUEST = $data;
// Access the data and perform operations
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
// Perform further processing or respond to the request
// ...
?>

在此示例中,使用 `file_get_contents('php://input')` 檢索 JSON POST 資料並將其儲存在 `$jsonData` 變數中。然後,使用 `json_decode()` 函式將 JSON 資料解碼為 PHP 關聯陣列,並將其儲存在 `$data` 變數中。

解碼後的資料被分配給 `$_REQUEST` 超全域性變數,使其可以作為關聯陣列訪問。然後,您可以使用相應的陣列鍵(例如,`$_REQUEST['name']`、`$_REQUEST['age']`)訪問接收到的資料,並根據您的特定需求執行任何必要的操作或處理。

請記住,在某些情況下,修改 `$_REQUEST` 超全域性變數是不推薦的,因為它結合了來自各種來源(GET、POST 和 COOKIE)的資料,這可能會引入安全風險。通常,根據資料的來源,使用特定的超全域性變數(`$_GET`、`$_POST` 或 `$_COOKIE`)更安全。

結論

這些方法提供了在 PHP 中接收和處理 JSON POST 請求的不同方法。方法的選擇取決於您的特定用例和偏好。第一種方法為您提供了更多控制和靈活性,而後兩種方法利用了 PHP 的內建功能來處理 JSON 資料。

更新於: 2023年8月1日

16K+ 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告