Tartalomjegyzék

< PHP REST API

PHP REST API adatbázisból (Fejlesztés alatt)

Projekt szerkezet

app01/
  |-api/
  |  |-create.php
  |  `-read.php
  |-core/
  |  `-init.php
  `-includes/
     |-config.php
     |-database.php
     `-mariadb.php   

Adatbázis

includes/config.php
$db['host'] = 'localhost';
$db['user'] = 'app01';
$db['pass'] = 'titok';
$db['name'] = 'app01';
includes/mariadb.php
 
includes/database.php
 

Előkészítés

init.php
define('DS', DIRECTORY_SEPARATOR);
define('SITE_ROOT', DS . 'xampp' . DS .'htdocs' . DS .'app01');
define('CORE_PATH', SITE_ROOT.DS.'core');
 
require_once(CORE_PATH.DS."database.php");

Lekérdezés

api/read.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
 
include_once('../core/init.php');

Létrehozás

api/create.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: POST');
 
include_once('../core/init.php');

Függelék

A kérések szétválogatása metódus alapján

index.php
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['REQUEST_URI'];
 
switch($method) {
    case 'POST':
        create_data($request);
        break;
    case 'GET':
        read_data();
        break;
    case 'PUT':
        update_data($request);
        break;
    case 'DELETE':
        delete_data($request);
        break;
    default:
        handle_error($request);
}
 
function create_data($request) {}
function read_data() {}
function update_data($request) {}
function delete_data($request) {}
function handle_error($request) {}