$dbh = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbh; } function validateApiKey($key) { $sql = "select * FROM tbl_api_reg where api_key='" . $key . "'"; $db = getConnection(); $sth = $db->prepare($sql); $sth->execute(); return $sth->rowCount(); } $authKey = function ($route) { $app = \Slim\Slim::getInstance(); $routeParams = $route->getParams(); if (validateApiKey($routeParams["key"]) == 0) { $app->halt(401); } }; $app->get('/customer/:key/', $authKey, function () use($app) { $sql = "select * FROM tbl_customer"; $response = $app->response(); $response['Content-Type'] = 'application/json'; $response['X-Powered-By'] = 'Gede Lumbung'; try { $db = getConnection(); $stmt = $db->query($sql); $data = $stmt->fetchAll(PDO::FETCH_OBJ); $db = null; $response->status(200); $response->body(json_encode(array('customer' => $data)));
$error_fields .= $field . ', '; } } if ($error) { $app = \Slim\Slim::getInstance(); $response = $app->response(); $response->write(json_encode(response('Required field(s) : ' . substr($error_fields, 0, -2) . ' is missing or empty', 'Missing Fields', false))); $app->stop(); } } $authKey = function (\Slim\Route $route) { $headers = apache_request_headers(); $app = \Slim\Slim::getInstance(); if (isset($headers['Authorization'])) { $key = $headers['Authorization']; if (validateApiKey($key) == 0) { $response = $app->response(); $response->write(json_encode(response('Api Key Error ', 'Authorization', false))); $app->stop(); } } else { $response = $app->response(); $response->write(json_encode(response('Api Key is missing ', 'Authorization', false))); $app->stop(); } }; //Require File require_once 'includes/require_params.php'; foreach (glob("routes/*.php") as $filename) { require_once $filename; }