sumber: https://rasupe.com/cara-membuat-rest-api-dengan-php-mysql-mudah-dan-cepat/
1. Create mysql database and tables
Open phpMyAdmin or whatever you usually use.
1.1 Create a new database with the name “db_toko”
1.2 Create a “categories” table.
- CREATE TABLE IF NOT EXISTS `categories` ( `cat_id` int(11) NOT NULL AUTO_INCREMENT, `cat_name` varchar(256) NOT NULL, `cat_description` text NOT NULL, `cat_created` datetime NOT NULL, `cat_modified` datetime NOT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Result:
INSERT INTO `categories` VALUES
(1, 'Pakaian', 'Kategori pakaian, baju dll', '2018-08-01 01:02:03', '2018-08-01 02:02:03'),
(2, 'Elektronik', 'HP, Tablet, Laptop, dll.', '2018-08-01 01:02:03', '2018-08-01 02:02:03'),
(3, 'Buku Komputer', 'Kategori Buku Komputer', '2018-08-01 01:02:03', '2018-08-01 02:02:03'),
(4, 'Film', 'Kategori film.', '2018-08-01 01:02:03', '2018-08-01 02:02:03'),
(5, 'Buku Sejarah', 'Kategori Buku Sejarah', '2018-08-01 01:02:03', '2018-08-01 02:02:03'),
(6, 'Olahraga', 'Kategori Olahraga.', '2018-08-01 01:02:03', '2018-08-01 02:02:03');
Result:
2. Create the PHP file structure
Suppose your server is at c:/xampp/htdocs/shop/ create a api subfolder in the store folder, the file structure is as below:
api/ db/ settings.ini.php Db.class.php Log.class.php categories/ list.php For files in the db folder can be downloaded here, edit the settings.ini.php file according to your server configuration. 3. Creating API files the contents of the list.php file as below:
<strong>limit</strong> format integer
<strong>name</strong> format string
limit=10 if you want to display 10 data name=book if you want to display the name of the book categoryurl dengan parameter http://localhost/toko/api/categories/list.php?limit=10&name=buku
hasilnya:
{“info”:”success”,”num”:2,”result”:[{“cat_id”:3,”cat_name”:”Buku Komputer”,”cat_description”:”Kategori Buku Komputer”,”cat_created”:”2018-08-01 01:02:03″,”cat_modified”:”2018-08-01 02:02:03″},{“cat_id”:5,”cat_name”:”Buku Sejarah”,”cat_description”:”Kategori Buku Sejarah”,”cat_created”:”2018-08-01 01:02:03″,”cat_modified”:”2018-08-01 02:02:03″}]}
When converted into an array, the result is as follows:
Komentar Terbaru