🔴 Advanced  ·  Lesson 37

REST API

REST API

What is a REST API?

A REST API lets other apps talk to your server using HTTP methods (GET, POST, PUT, DELETE) and returns data as JSON.

Simple REST Endpoint

header("Content-Type: application/json");
$method = $_SERVER["REQUEST_METHOD"];

if ($method === "GET") {
    echo json_encode(["users" => ["Aman", "Riya"]]);
} elseif ($method === "POST") {
    $input = json_decode(file_get_contents("php://input"), true);
    echo json_encode(["created" => $input]);
}

HTTP Methods

MethodUse
GETread data
POSTcreate data
PUTupdate data
DELETEremove data

Summary

  • A REST API uses HTTP methods and returns JSON.
  • Read the method from $_SERVER["REQUEST_METHOD"] and respond with json_encode.

REST API क्या है?

REST API दूसरे apps को HTTP methods (GET, POST, PUT, DELETE) से आपके server से बात करने देता है और data JSON में लौटाता है।

Simple REST Endpoint

header("Content-Type: application/json");
$method = $_SERVER["REQUEST_METHOD"];

if ($method === "GET") {
    echo json_encode(["users" => ["Aman", "Riya"]]);
} elseif ($method === "POST") {
    $input = json_decode(file_get_contents("php://input"), true);
    echo json_encode(["created" => $input]);
}

HTTP Methods

MethodUse
GETdata पढ़ना
POSTdata बनाना
PUTdata update
DELETEdata हटाना

सारांश

  • REST API HTTP methods use करता है और JSON लौटाता है।
  • Method $_SERVER["REQUEST_METHOD"] से पढ़ें और json_encode से respond करें।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।