Pilih Laman

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:
<?php
header(‘Content-Type: application/json’);
include dirname(dirname(__FILE__)).‘/db/Db.class.php’;
$db = new Db();
$limit = isset($_GET[‘limit’]) ? (int) $_GET[‘limit’] : 0;
$name = isset($_GET[‘name’]) ? $_GET[‘name’] : ;
$sql_limit = ;
if (!empty($limit)) {
$sql_limit = ‘ LIMIT 0,’.$limit;
}
$sql_name = ;
if (!empty($name)) {
$sql_name = ‘ where cat_name LIKE \’%’.$name.‘%\’ ‘;
}
$cat_list = $db->query(‘select * from categories ‘.$sql_name.‘ ‘.$sql_limit);
$arr = array();
$arr[‘info’] = ‘success’;
$arr[‘num’] = count($cat_list);
$arr[‘result’] = $cat_list;
echo json_encode($arr);
4. Attempting to call the API
What we use for the API URL is:  http://localhost/toko/api/categories/list.php
Parameter:
<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 category
The method used is GET So in full the api URL and the parameters to call the category list are: http://localhost/toko/api/categories/list.php?limit=10&name=buku Please open the browser and see the results. url without parameter http://localhost/toko/api/categories/list.php results:
{“info”:”success”,”num”:6,”result”:[{“cat_id”:1,”cat_name”:”Pakaian”,”cat_description”:”Kategori pakaian, baju dll”,”cat_created”:”2018-08-01 01:02:03″,”cat_modified”:”2018-08-01 02:02:03″},{“cat_id”:2,”cat_name”:”Elektronik”,”cat_description”:”HP, Tablet, Laptop, dll.”,”cat_created”:”2018-08-01 01:02:03″,”cat_modified”:”2018-08-01 02:02:03″},{“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”:4,”cat_name”:”Film”,”cat_description”:”Kategori film.”,”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″},{“cat_id”:6,”cat_name”:”Olahraga”,”cat_description”:”Kategori Olahraga.”,”cat_created”:”2018-08-01 01:02:03″,”cat_modified”:”2018-08-01 02:02:03″}]}

url 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:

Array
(
[info] => success
[num] => 2
[result] => Array
(
[0] => Array
(
[cat_id] => 3
[cat_name] => Buku Komputer
[cat_description] => Kategori Buku Komputer
[cat_created] => 20180801 01:02:03
[cat_modified] => 20180801 02:02:03
)
[1] => Array
(
[cat_id] => 5
[cat_name] => Buku Sejarah
[cat_description] => Kategori Buku Sejarah
[cat_created] => 20180801 01:02:03
[cat_modified] => 20180801 02:02:03
)
)
)
5. Done