REST

نمائندگي واري رياست منتقلي

REST is the acronym for نمائندگي واري رياست منتقلي.

ڇا آھي نمائندگي واري رياست منتقلي?

An architectural style for designing networked applications. In virtually all cases, it relies on a stateless, client-server, cacheable communications protocol, the HTTP protocol. The idea behind REST is to treat all server-side resources as objects that can be created, read, updated, or deleted through a set of defined operations. This concept aligns closely with the standard operations supported by HTTP: POST, GET, PUT, and DELETE.

Why It’s Called Representational State Transfer

هن اصطلاح نمائندگي واري رياست منتقلي is chosen for specific reasons:

  • Representational refers to resource representations (the document or the object you requested from the server) transferred over the network. The client can easily handle these representations in formats like كل, JSON، يا يام ايل.
  • State Transfer implies that every client and server interaction transfers a state. When a client requests a resource, the server’s response is essentially transferring the state of that resource to the client. This state transfer allows a RESTful application to be stateless, meaning that each request from a client to a server must contain all the information needed to understand and complete the request. The server does not store any state about the client session on the server side.

Principles of REST

REST is built on several key principles which define its simplicity and power:

  1. بي وطن: Each request from client to server must contain all the information necessary to understand and complete the request. The server has no session state; it is kept entirely on the client’s side.
  2. ڪلائنٽ-سرور: A uniform interface separates clients from servers. This separation of concerns supports the independent evolution of the client-side logic and server-side data storage, improving the portability of the client interface across multiple platforms.
  3. Cacheable: Responses must define themselves as cacheable or not to prevent clients from reusing stale or inappropriate data in response to further requests.
  4. پرت وارو نظام: A client cannot ordinarily tell whether it is connected directly to the end server or to an intermediary. Intermediary servers can improve system scalability by enabling load balancing and providing shared caches.
  5. يونيفارم انٽرفيس: To obtain the benefits of REST, applications must adhere to a uniform interface. This typically involves using standard HTTP methods in a consistent way and following resource-oriented URLs.

PHP Example

Creating a RESTful API in PHP involves handling HTTP requests (GET, POST, PUT, DELETE) and responding with data in a format like JSON or XML. Here’s a simplified example of a RESTful API in PHP that manages a list of tasks. This example demonstrates handling GET and POST requests for simplicity.

هي پي PHP حفاظتي example will show you how to create two endpoints: one for retrieving the list of tasks (GET /tasks) and another for adding a new task (POST /tasks).

index.php – The Entry Point

<?php
// Define a simple array of tasks as our "database"
$tasks = [
    ['id' => 1, 'title' => 'Buy groceries', 'completed' => false],
    ['id' => 2, 'title' => 'Finish homework', 'completed' => false]
];

// Get the request method
$requestMethod = $_SERVER['REQUEST_METHOD'];

// Simple router
switch ($requestMethod) {
    case 'GET':
        getTasks();
        break;
    case 'POST':
        addTask();
        break;
    default:
        // Handle other HTTP methods or return an error
        header('HTTP/1.1 405 Method Not Allowed');
        break;
}

function getTasks() {
    global $tasks;
    header('Content-Type: application/json');
    echo json_encode($tasks);
}

function addTask() {
    global $tasks;
    $input = json_decode(file_get_contents('php://input'), true);
    if (!isset($input['title']) || !isset($input['completed'])) {
        header('HTTP/1.1 400 Bad Request');
        echo json_encode(['message' => 'Missing title or completed status']);
        return;
    }

    $newTask = [
        'id' => end($tasks)['id'] + 1,
        'title' => $input['title'],
        'completed' => $input['completed']
    ];

    $tasks[] = $newTask;
    header('Content-Type: application/json');
    echo json_encode($newTask);
}

?>

ڪيئن اهو ڪم

  • This script acts as a simple API endpoint. Depending on the HTTP request method, it either returns a list of tasks (GET) or adds a new task to the list (POST).
  • لاء GET requests, it simply outputs the $tasks array in JSON format.
  • لاء POST requests, it reads the JSON payload from the request body (assumed to contain title ۽ completed status), adds a new task to the $tasks array, and returns the new task as JSON.
  • This example uses a PHP global array as a mock database. In a real-world application, you would likely interact with a database to store and retrieve tasks.

API جي جاچ ڪندي

You can test this API using tools like Postman or cURL. For example, to add a new task:

curl -X POST -H "Content-Type: application/json" -d '{"title":"Learn REST","completed":false}' http://localhost/index.php

And to get the list of tasks:

curl -X GET http://localhost/index.php

This is a very basic example meant to illustrate the concept of a RESTful API in PHP. Real-world scenarios would require more robust handling of requests, error management, and security considerations such as authentication and input validation.

  • مختصر REST
مٿي تي ڪلڪ ڪري بٽڻ
بند

Adblock جو پتو لڳايو ويو

Martech Zone توهان کي هي مواد بغير ڪنهن قيمت تي مهيا ڪرڻ جي قابل آهي ڇو ته اسان اسان جي سائيٽ کي اشتهارن جي آمدني، الحاق لنڪس، ۽ اسپانسرشپ ذريعي رقم ڪريون ٿا. اسان جي تعريف ڪنداسين جيڪڏهن توهان پنهنجي اشتهار بلاڪ ڪندڙ کي هٽائي ڇڏيو جيئن توهان اسان جي سائيٽ کي ڏسو.