← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 6 of 24

PHP REST API Basics

एक REST API एक restaurant menu जैसा है जिससे programs simple web requests इस्तेमाल करके order कर सकते हैं। हर address किसी चीज़ के लिए खड़ा है, जैसे users, और get या delete जैसे verbs बताते हैं कि क्या करना है।
Syntax
php
header("Content-Type: application/json");
http_response_code(200);
echo json_encode($data);

एक REST API क्या है?

एक REST API आपके application का data plain HTTP पर expose करता है, दूसरे programs (एक mobile app, एक JavaScript frontend, कोई दूसरा server) को एक custom protocol के बजाय GET और POST जैसे standard verbs इस्तेमाल करके resources पढ़ने और modify करने देते हुए।

उदाहरण: What is a REST API?

php
<?php
// Call `header("Content-Type: application/json")`
header("Content-Type: application/json");
// Print `json_encode(["message" => "REST API responds over plain HTTP"])` to the output
echo json_encode(["message" => "REST API responds over plain HTTP"]);
?>

एक GET Endpoint बनाना

एक minimal GET endpoint request की गई data पढ़ता है, इसे json_encode() से encode करता है, और इसे print करता है -- दूसरे छोर पर client बस उस JSON response को parse कर लेता है।

उदाहरण: Creating a GET Endpoint

php
<?php
// Declare `$users` as an array: `[["id" => 1, "name" => "Alice"]]`
$users = [["id" => 1, "name" => "Alice"]];
// Print `json_encode($users)` to the output
echo json_encode($users);
?>

HTTP Response Headers Set करना

हर JSON API response को header() के through अपना Content-Type header application/json set करना चाहिए, ताकि consuming code को पता चले कि body को खुद content से guess करने के बजाय JSON की तरह parse करना है।

उदाहरण: Setting HTTP Response Headers

php
<?php
// Call `header("Content-Type: application/json")`
header("Content-Type: application/json");
// Print `json_encode(["status" => "ok"])` to the output
echo json_encode(["status" => "ok"]);
?>

Request Methods पढ़ना

$_SERVER[REQUEST_METHOD] check करना आपकी script को बताता है कि आने वाली call GET, POST, PUT, या DELETE है, जो है कि एक single endpoint script same resource पर कई अलग operations handle करने के लिए कैसे branch कर सकती है।

उदाहरण: Reading Request Methods

php
<?php
// Set `$_SERVER['REQUEST_METHOD']` to 'DELETE'
$_SERVER['REQUEST_METHOD'] = 'DELETE';
// Switch on `$_SERVER['REQUEST_METHOD']` and branch by matching case
switch ($_SERVER['REQUEST_METHOD']) {
    // Case: `'GET'`
    case 'GET':
        // Print "Reading data" to the output
        echo "Reading data";
        break;
    // Case: `'DELETE'`
    case 'DELETE':
        // Print "Deleting resource" to the output
        echo "Deleting resource";
        break;
}
?>

Request Payloads पढ़ना

POST और PUT requests के लिए, submitted data आमतौर पर $_POST में बिल्कुल नहीं होता अगर यह raw JSON की तरह भेजा गया हो -- आप इसे सीधे file_get_contents('php://input') से request body से पढ़ते हैं और खुद decode करते हैं।

उदाहरण: Reading Request Payloads

php
<?php
file_put_contents("php_input_sim.json", '{"name": "Alice"}');
$raw = file_get_contents("php_input_sim.json"); // stands in for php://input
$data = json_decode($raw, true);
echo $data['name'];
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. JSON output भेजने से पहले Content-Type: application/json set करना भूल जाना।
  2. $_SERVER[REQUEST_METHOD] को ignore करना, ताकि एक GET और एक POST same तरीके से handle हों।
  3. errors के लिए भी हमेशा status 200 return करना, 404 या 400 जैसे codes के बजाय।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.