<?php
header('Content-type: application/json');
include("App.php");
/**
* User: Darryl
* Date: 26/04/13
* Time: 15:17
*/
$request = $_SERVER['REQUEST_URI'];
$request = explode("/", $request);
foreach ($request as $key => $value) {
}
if (isset($_POST['apikey'])) {
$apikey = $_POST['apikey'];
} else {
$apikey = "null";
$return_array['code'] = "404";
$return_array['msg'] = "method not found";
$return_array['apikey'] = $apikey;
$return_array['data'] = "";
$return_array['authorised'] = false;
}
if ($apikey == "12345") {
$return_array['authorised'] = true;
} else {
$return_array['code'] = "404";
$return_array['msg'] = "invalid api key";
$return_array['authorised'] = false;
echo json_encode($return_array);
return false;
}
if ($return_array['authorised'] == true) {
if ($request[2] == "request") {
switch ($request[3]) {
case "login":
$tryLogin = App::checkLogin($_POST['email'], $_POST['password']);
if ($tryLogin == false) {
$return_array['code'] = "400";
$return_array['msg'] = "login checked, failed";
$return_array['data'] = false;
} else {
$return_array['code'] = "100";
$return_array['msg'] = "login checked, returning result";
$return_array['data'] = $tryLogin;
}
break;
default:
$return_array['code'] = "404";
$return_array['msg'] = "method not found";
break;
}
} else {
$return_array['code'] = "404";
$return_array['msg'] = "method not found";
$return_array['apikey'] = $apikey;
$return_array['data'] = "";
}
} else {
$return_array['msg'] = "Unauthorised request to API";
$return_array['code'] = "500";
}
//generate json array
echo json_encode($return_array);
?>
<?php
/**
* Created by PhpStorm.
* User: Darryl
* Date: 26/04/14
* Time: 15:17
*/
include("../classes/Config.php");
include("../classes/DB.php");
// Standard includes
function hashPassword($password)
{
return hash("sha512", $password);
}
class App
{
public static function checkLogin($email, $password)
{
if ($email != "" && $password != "") {
$email = htmlentities(strtolower($email));
$password = hashPassword($password);
$sql = "SELECT * FROM `users` WHERE `email` = :email AND `password` = :password";
$core = DB::getInstance();
$stmt = $core->dbh->prepare($sql);
$result = $stmt->execute(array(
':email' => $email,
':password' => $password
));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$stmt) {
print_r($core->dbh->errorInfo());
}
if ($row) {
$user = array();
$user["email"] = $row[0]['email'];
$user["forename"] = $row[0]['forename'];
$user["surname"] = $row[0]['surname'];
$user["level"] = $row[0]['level'];
$user["avatar"] = $row[0]['avatar'];
$user["confirmed"] = $row[0]['confirmed'];
return $user;
} else {
$fail_message = "invalid email / password";
return false;
}
} else {
$null_message = "Error";
return false;
}
}
}
?>
I'd recommend webapi2 rather than php.
hehe ok this is way over my head should do more googling i guess. Maybe its not an API needed? but this is a quick breakdown of what im trying to achieve.
Basically i was looking to take an online order lets say of 3 items it email a print server and print 2 sheets. then to produce a bar code/number code to then input that into epos to then deduct the 3 items from the stock system.
I knew this was going to be way over my head but though if i started to learn this kind of thing then i may be able to explain/help make something.
Just started writing an API in Go today, really simple using mux.
Aye, I take it your talking about Gorilla mux?
There's faster routers, but lets be honest unless your serving stack overflow levels of traffic Gorilla mux is a nice simple but flexible router.
Yeah, its an internal developer API for putting JSON and mustache templates into Amazon S3, maybe a few requests a second with some bursts of up to 20 when Jenkins pushes to it.