Skip to content

Instantly share code, notes, and snippets.

@timint
Last active May 12, 2020
Embed
What would you like to do?
Simple concept for a LiteCart REST API
<?php
ob_start();
include('includes/app_header.inc.php');
try {
$json = [];
switch(true) {
//--------------------------------------------------------------------
// Return A List Of Orders
//--------------------------------------------------------------------
case (preg_match('#/orders$#', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))):
if ($_SERVER['REQUEST_METHOD'] != 'GET') {
throw new Exception('This resource does only support HTTP GET requests', 400);
}
$orders_query = database::query(
"SELECT o.*, oi.name as order_status_name FROM ". DB_TABLE_ORDERS ." o
LEFT JOIN ". DB_TABLE_ORDER_STATUSES_INFO ." oi on (oi.order_status_id = o.order_status_id and oi.language_code = '". database::input(language::$selected['code']) ."')
ORDER BY o.date_created DESC;"
);
while ($row = database::fetch($orders_query)) {
$json[] = [
'id' => $row['id'],
'order_status' => ['id' => $row['order_status_id'], 'name' => $row['order_status_name']],
'total_amount' => (float)$row['payment_due'],
'total_weight' => weight::convert($row['weight_total'], $row['weight_class'], 'kg'),
'updated_at' => date('c', strtotime($row['date_updated'])),
'created_at' => date('c', strtotime($row['date_created'])),
];
}
break;
//--------------------------------------------------------------------
// Return An Order
//--------------------------------------------------------------------
case (preg_match('#/orders/([0-9]+)$#', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), $matches)):
$order_query = database::query(
"SELECT id FROM ". DB_TABLE_ORDERS ."
WHERE id = '". database::input($matches[1]) ."'
LIMIT 1;"
);
if (!$row = database::fetch($order_query)) {
throw new Exception('Could not find order in database', 404);
}
$order = new ent_order($row['id']);
$json = $order->data;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!$data = file_get_contents('php://input')) {
throw new Exception('Invalid request body data', 400);
}
if (!$data = json_decode($data, true)) {
throw new Exception('Invalid regquest json data', 400);
}
$order->data = array_replace($order->data, array_intersect_key($delivery_status, $order->data));
$order->save();
$json = ['status' => 'ok'];
}
break;
//--------------------------------------------------------------------
default:
throw new Exception('Unknown resource', 404);
}
if ($buffer = ob_get_clean()) {
throw new Exception('Unexpected buffer output: '. $buffer);
}
http_response_code(200);
} catch (Exception $e) {
http_response_code($e->getCode());
$json = ['error' => $e->getMessage()];
}
//if (isset($_GET['pretty_print'])) {
$json = json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
//} else {
// $json = json_encode($json, JSON_UNESCAPED_SLASHES);
//}
if ($json === false) {
$json = '{"error":"Unknown error while encoding JSON"}';
}
header('Date: '. date('r'));
header('Content-Type: application/json; charset=UTF-8');
header('Content-Length: '. strlen($json));
echo $json;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment