PHP REST API Basics
In this page:
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
// 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"]);
?>
Login to try C/C++/Java/PHP code in the editor
एक GET Endpoint बनाना
एक minimal GET endpoint request की गई data पढ़ता है, इसे json_encode() से encode करता है, और इसे print करता है -- दूसरे छोर पर client बस उस JSON response को parse कर लेता है।
उदाहरण: Creating a GET Endpoint
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// Call `header("Content-Type: application/json")`
header("Content-Type: application/json");
// Print `json_encode(["status" => "ok"])` to the output
echo json_encode(["status" => "ok"]);
?>
Login to try C/C++/Java/PHP code in the editor
Request Methods पढ़ना
$_SERVER[REQUEST_METHOD] check करना आपकी script को बताता है कि आने वाली call GET, POST, PUT, या DELETE है, जो है कि एक single endpoint script same resource पर कई अलग operations handle करने के लिए कैसे branch कर सकती है।
उदाहरण: Reading Request Methods
<?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;
}
?>
Login to try C/C++/Java/PHP code in the editor
Request Payloads पढ़ना
POST और PUT requests के लिए, submitted data आमतौर पर $_POST में बिल्कुल नहीं होता अगर यह raw JSON की तरह भेजा गया हो -- आप इसे सीधे file_get_contents('php://input') से request body से पढ़ते हैं और खुद decode करते हैं।
उदाहरण: Reading Request Payloads
<?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'];
?>
Login to try C/C++/Java/PHP code in the editor
- JSON output भेजने से पहले
Content-Type: application/jsonset करना भूल जाना। $_SERVER[REQUEST_METHOD]को ignore करना, ताकि एक GET और एक POST same तरीके से handle हों।- errors के लिए भी हमेशा status 200 return करना, 404 या 400 जैसे codes के बजाय।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser