A
A
Anton2021-04-04 09:30:42
RESTful API
Anton, 2021-04-04 09:30:42

How to do a task?

You need to develop your own service (API), which will implement the basic data access functionality using the REST style. I made it on slim.
And then it says - "the maximum possible range of HTTP statuses should be used." What does this mean? Previously, I didn’t climb into the rest at all and had no idea how to finish it.
Ie these are http response codes?
https://developer.mozilla.org/ru/docs/Web/HTTP/Status
How to work with them? I would appreciate at least links or examples.
The code:

$app->group('/api', function (RouteCollectorProxy $group) {
    $group->get('/products[/]',function(Request $request, Response $response){
        $sql = "SELECT * FROM products";
        try {
            $db = new DB();
            $conn =  $db->connect();
            $stmt = $conn->query($sql);
            $products = $stmt->fetchAll(PDO::FETCH_OBJ);
    
            $db = null;
            $response->getBody()->write(json_encode($products));
            return $response
             ->withHeader('content-type', 'application/json')
             ->withStatus(200);
        } catch (PDOException $e){
            $error = array(
            "message" => $e->getMessage()
            );
        $response->getBody()->write(json_encode($error));
        return $response
        ->withHeader('content-type', 'application/json')
        ->withStatus(500);
        }
    });
    $group->get('/products/{id}/',function(Request $request, Response $response,array $args){
        $id = $args['id'];
        $sql = "SELECT * FROM products WHERE id = $id";
        try {
            $db = new DB();
            $conn =  $db->connect();
            $stmt = $conn->query($sql);
            $product = $stmt->fetch(PDO::FETCH_OBJ);
    
            $db = null;
            $response->getBody()->write(json_encode($product));
            return $response
             ->withHeader('content-type', 'application/json')
             ->withStatus(200);
        } catch (PDOException $e){
            $error = array(
            "message" => $e->getMessage()
            );
        $response->getBody()->write(json_encode($error));
        return $response
        ->withHeader('content-type', 'application/json')
        ->withStatus(500);
        }
    });
    $group->post('/products[/]',function(Request $request, Response $response,array $args){
        $parameters = json_decode($request->getBody(), TRUE);
        $product_name = $parameters['product_name'];
        $product_price = $parameters['product_price'];
        $product_count = $parameters['product_count'];
        $sql = "INSERT INTO products (product_name,product_price,product_count) VALUES(:product_name,:product_price,:product_count)";
        try {
            $db = new DB();
            $conn =  $db->connect();
            $stmt = $conn->prepare($sql);
            $stmt->bindParam(':product_name',$product_name);
            $stmt->bindParam(':product_price',$product_price);
            $stmt->bindParam(':product_count',$product_count);
            $result = $stmt->execute();    
            $db = null;
            $response->getBody()->write(json_encode($result));
            return $response
             ->withHeader('content-type', 'application/json')
             ->withStatus(200);
        } catch (PDOException $e){
            $error = array(
            "message" => $e->getMessage()
            );
        $response->getBody()->write(json_encode($error));
        return $response
        ->withHeader('content-type', 'application/json')
        ->withStatus(500);
        }
    });
    $group->put('/products/{id}/',function(Request $request, Response $response,array $args){
        $parameters = json_decode($request->getBody(), TRUE);
        $id = $parameters['id'];
        $product_name = $parameters['product_name'];
        $product_price = $parameters['product_price'];
        $product_count = $parameters['product_count'];
        $sql = "UPDATE products SET product_name =:product_name , product_price=:product_price,product_count=:product_count WHERE id = $id";
        try {
            $db = new DB();
            $conn =  $db->connect();
            $stmt = $conn->prepare($sql);
            $stmt->bindParam(':product_name',$product_name);
            $stmt->bindParam(':product_price',$product_price);
            $stmt->bindParam(':product_count',$product_count);
            $result = $stmt->execute();
    
            $db = null;
            $response->getBody()->write(json_encode($result));
            return $response
             ->withHeader('content-type', 'application/json')
             ->withStatus(200);
        } catch (PDOException $e){
            $error = array(
            "message" => $e->getMessage()
            );
        $response->getBody()->write(json_encode($error));
        return $response
        ->withHeader('content-type', 'application/json')
        ->withStatus(500);
        }
});
    $group->delete('/products/{id}/',function(Request $request, Response $response,array $args){
        $id = $args['id'];
        $sql = "DELETE FROM products WHERE id = $id";
        try {
            $db = new DB();
            $conn =  $db->connect();
            $stmt = $conn->prepare($sql);
            $result = $stmt->execute();
    
            $db = null;
            $response->getBody()->write(json_encode($result));
            return $response
             ->withHeader('content-type', 'application/json')
             ->withStatus(200);
        } catch (PDOException $e){
            $error = array(
            "message" => $e->getMessage()
            );
        $response->getBody()->write(json_encode($error));
        return $response
        ->withHeader('content-type', 'application/json')
        ->withStatus(500);
        }
});
// var_dump($_SERVER['REQUEST_METHOD']);
 });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aleks-th, 2021-04-04
@Miron9900

Tasks to be done:
1. Independently. You need to learn, not other people.
2. If you don’t want to do it yourself, then the freelance button is at the top of the menu.
Well, or at least read the help before asking others to read and think for you.
And to formulate questions more precisely.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question