api를 만들어봤는데 이걸 api라고 할 수 있을까요..?
본문
postman으로 아래와 같이 GET/POST/PUT/DELETE 테스트를 해서 정상적으로 작동을 하게끔 했습니다..
GET https://domain.com/api/member.php?id=dg01&password=1234
POST https://domain.com/api/member.php Body->form-data 로 값 전달
PUT https://domain.com/api/member.php Body->raw->JSON 로 값 전달
DELETE https://domain.com/api/member.php Body->raw->JSON 로 값 전달
include $_SERVER['DOCUMENT_ROOT']."/lib_inc.php";
include "./jwt_inc.php";
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$jwt = new JWT();
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if(isset($_GET['id']) && isset($_GET['password']) && isset($_GET['token'])){
...................
if($itemCount > 0){
if(password_verify($password, $row['mt_pwd'])) {
$token = $_GET['token'];
// jwt에서 유저 정보 가져오기
$data = $jwt->dehashing($token);
if($data['type'] === "time"){
http_response_code(404);
echo json_encode("time error");
break;
}
if($data['type'] === "key"){
http_response_code(404);
echo json_encode("key error");
break;
}
$parted = explode('.', base64_decode($token));
$payload = json_decode($parted[1], true);
$mt_id = base64_decode($payload['id']);
$mt_pwd = base64_decode($payload['password']);
$mt_pwd_re = base64_decode($payload['password']);
if($mt_id != $_GET['id']){
http_response_code(404);
echo json_encode("아이디가 다릅니다.");
break;
}
.............
http_response_code(200);
echo json_encode($emp_arr);
} else {
http_response_code(404);
echo json_encode("id not found.");
}
}else{
http_response_code(404);
echo json_encode("id not found.");
}
}else{
http_response_code(404);
echo json_encode("id not found.");
}
break;
case 'POST':
//raw 받을때 아래줄사용
//$jsonData = json_decode(file_get_contents("php://input"), true);
// print_r($jsonData);
// echo "////";
// print_r($jsonData['vact']["banks"][0]);
//break;
if(isset($_POST['mt_name']) && isset($_POST['mt_id']) && isset($_POST['mt_pwd']) && isset($_POST['mt_pwd_re']) && isset($_POST['mt_team'])){
if($_POST['mt_pwd']!=$_POST['mt_pwd_re']) {
http_response_code(404);
echo json_encode("비밀번호가 다릅니다.");
break;
}
if($row['id']) {
http_response_code(404);
echo json_encode("이미 존재하는 회원입니다.");
break;
}
....................
http_response_code(200);
echo json_encode($emp_arr);
echo 'Employee created successfully.';
}else{
echo 'Employee could not be created.';
}
}else{
http_response_code(404);
echo json_encode("id not found.");
}
break;
case 'PUT':
$jsonData = json_decode(file_get_contents("php://input"), true);
//print_r($jsonData['member_info']);
$token = $jsonData['member_info']["mt_token"];
$data = $jwt->dehashing($token);
if($data['type'] === "time"){
http_response_code(404);
echo json_encode("time error");
break;
}
if($data['type'] === "key"){
http_response_code(404);
echo json_encode("key error");
break;
}
$parted = explode('.', base64_decode($token));
$payload = json_decode($parted[1], true);
$mt_id = base64_decode($payload['id']);
$mt_pwd = base64_decode($payload['password']);
$mt_pwd_re = base64_decode($payload['password']);
if($mt_id != $jsonData['member_info']['mt_id']){
http_response_code(404);
echo json_encode("아이디가 다릅니다.");
break;
}
if(isset($mt_id) && isset($mt_pwd) && isset($mt_pwd_re) && isset($jsonData['member_info']['mt_status'])){
if($jsonData['member_info']['mt_pwd']!=$jsonData['member_info']['mt_pwd_re']) {
http_response_code(404);
echo json_encode("비밀번호가 다릅니다.");
break;
}
...................
if($row['cnt'] < 1) {
http_response_code(404);
echo json_encode("존재하지 않는 회원입니다.");
break;
}
if(password_verify($password, $row['mt_pwd'])) {
......................
);
http_response_code(200);
echo json_encode($emp_arr);
echo json_encode("Employee data updated.");
}else{
http_response_code(404);
echo json_encode("Data could not be updated!!");
}
}else{
echo json_encode("Data could not be updated@@");
}
}else{
http_response_code(404);
echo json_encode("Data could not be updated");
}
break;
case 'DELETE':
$jsonData = json_decode(file_get_contents("php://input"), true);
...........
if($row['cnt'] < 1){
http_response_code(200);
echo json_encode("Employee delete.");
}else{
http_response_code(404);
echo json_encode("Data could not delete");
}
}else{
echo json_encode("비밀번호가 틀립니다.");
}
}else{
http_response_code(404);
echo json_encode("Data could not be updated");
}
break;
}
답변 2
정통방식의 rest api는 아니지만
그래도 데이터를 주고 받는 부분에서는 api가 맞다고 할 수 있습니다.
정통방식이라하면
get,post,put,delete 등
url체계부터 조금 차이가 있습니다.
PHP로 api를 만들때는
composer를 설치하시고
slim4 composer 등을 사용하시면 도움됩니다.
네.
답변을 작성하시기 전에 로그인 해주세요.