php - PHP:发送带有空正文的 POST 请求
问题描述
我被分配了一个小任务。该任务正在制作一个接受(GET、POST、PUT、DELETE)请求的简单 REST API。但是我必须处理一个小条件,即 POST 并且 PUT 请求必须接受带有空正文的请求。我该如何处理。
这是我的 update.php 文件:
<?php
// Setting Headers.
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: PUT');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
include_once '../../config/Database.php';
include_once '../../models/Student.php';
// Instantiating Database Object & Connection.
$database = new Database();
$student = new Student($database -> connect());
// Getting Raw Student Data.
$data = json_decode(file_get_contents("php://input"));
// Setting ID To Update.
$student -> id = $data -> id;
$student -> id = $data -> id;
$student -> fullName = $data -> fullName;
$student -> birthDate = $data -> birthDate;
$student -> birthplace = $data -> birthplace;
// Updatting The Student Information.
if( $student -> updateStudent() )
{
echo json_encode
(
array('message' => 'Student Has Been Updated.')
);
}
else
{
echo json_encode
(
array('message' => 'Student Has NOT Been Updated.')
);
}
这是处理 PUT 请求的 updateStudent() 方法:
// Updating (PUT) Students.
public function updateStudent()
{
// Creating Query.
$query = 'UPDATE ' . $this->table . ' SET id = :id, fullName = :fullName, birthDate = :birthDate, birthplace = :birthplace WHERE id = :id';
// Prepare Statement.
$statment = $this-> connection -> prepare($query);
// Cleaning Data.
$this -> id = htmlspecialchars(strip_tags($this -> id));
$this -> fullName = htmlspecialchars(strip_tags($this -> fullName));
$this -> birthDate = htmlspecialchars(strip_tags($this -> birthDate));
$this -> birthplace = htmlspecialchars(strip_tags($this -> birthplace));
// Binding data.
$statment -> bindParam(':id', $this -> id);
$statment -> bindParam(':fullName', $this -> fullName);
$statment -> bindParam(':birthDate', $this -> birthDate);
$statment -> bindParam(':birthplace', $this -> birthplace);
// Executing Query.
if($statment -> execute())
{
return true;
}
// Print Error If Something Goes Wrong.
printf("Error: %s.\n", $statment -> error);
return false;
}
这里不需要写处理 POST 请求的方法,因为它与 PUT 请求处理方法相同。
这是我发送带有数据的 PUT 请求时的响应:
{
"message": "Student Has Been Updated."
}
这是我发送 PUT 请求空正文时的响应:
<br />
<b>Notice</b>: Trying to get property 'id' of non-object in <b>C:\My Environment\My Programming Environment\XAMPP\htdocs\PHP_REST_API_Student_Automation_System\API\student\add.php</b> on line <b>20</b><br />
<br />
<b>Notice</b>: Trying to get property 'fullName' of non-object in <b>C:\My Environment\My Programming Environment\XAMPP\htdocs\PHP_REST_API_Student_Automation_System\API\student\add.php</b> on line <b>21</b><br />
<br />
<b>Notice</b>: Trying to get property 'birthDate' of non-object in <b>C:\My Environment\My Programming Environment\XAMPP\htdocs\PHP_REST_API_Student_Automation_System\API\student\add.php</b> on line <b>22</b><br />
<br />
<b>Notice</b>: Trying to get property 'birthplace' of non-object in <b>C:\My Environment\My Programming Environment\XAMPP\htdocs\PHP_REST_API_Student_Automation_System\API\student\add.php</b> on line <b>23</b><br />
解决方案
推荐阅读
- vba - VBA 应用程序定义或对象定义错误
- javascript - jQuery droppable:over 事件中不正确的放置元素
- vba - Excel VBA:循环输入框并将值添加到单元格范围
- c# - ASP.NET 4.5 或替代框架中的 OpenIdDict?
- matlab - 使用 GUIDE 将半透明热图叠加到图像上
- windows - 使用 PowerShell 中的 WinSCP .NET 程序集将 X 天之前的日志/文件从 Unix SFTP 服务器复制到 Windows
- multithreading - 线程中的 QSerialPort
- java - 来自 marklogic 休息端点的二进制数据
- spring - 如何从 Docker 'run' 命令设置 Spring Boot 属性
- python - 如何使用 python-cloudant 进行对象映射