PHP:发送带有空正文的 POST 请求

时间:2021-03-27 10:39:05

标签: php post put restapi

我被分配了一项小任务。任务是制作一个简单的 REST API,它接受(GET、POST、PUT、DELETE)请求。但是我必须处理一个小条件,即 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 />

0 个答案:

没有答案