RESTful API Development in PHP

 RESTful API Development in PHP


PHP can be used to develop RESTful APIs that can be used by other applications. PHP provides built-in functions and libraries to handle HTTP requests and responses.

Example:


php


// define a route for GET requests to /users

if ($_SERVER["REQUEST_METHOD"] == "GET" && $_SERVER["REQUEST_URI"] == "/users") {

    // retrieve data from the database

    $sql = "SELECT * FROM users";

    $result = mysqli_query($conn, $sql);

    $users = array();

    while ($row = mysqli_fetch_assoc($result)) {

        $users[] = $row;

    }

    // send the response as JSON

    header("Content-Type: application/json");

    echo json_encode($users);

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...