Exemplo n.º 1
0
<?php

include_once "ItemManager.php";
$SALT = "skatingatonementshark";
$method = $_SERVER['REQUEST_METHOD'];
$ip = $_SERVER['REMOTE_ADDR'];
$toHash = $ip . $SALT;
$expectedToken = hash('sha256', $toHash);
$token = $_SERVER['HTTP_TOKEN'];
if ($token === $expectedToken) {
    switch ($method) {
        case "GET":
            if (isset($_GET['id']) && !empty($_GET['id'])) {
                //getting record by id
                $id = $_GET['id'];
                $item = ItemManager::GetById($id);
                echo json_encode($item);
            } else {
                //getting all records
                $items = ItemManager::GetAllItems();
                echo json_encode($items);
            }
            break;
        case "PUT":
            $newRecordData = fopen("php://input", "r");
            $data = stream_get_contents($newRecordData);
            fclose($newRecordData);
            parse_str($data);
            $newId = ItemManager::AddNewItem($desc, $price, $quantity);
            echo "The item {$desc} has been inserted with the id {$newId}.";
            break;