php-json-server
is RESTful API with PHP. The data can be wrote json format, and you can easily set up RESTful API mock server.
Use Composer for installation and server start-up.
# install
$ composer create-project sawarame/php-json-server path/to/install
Once installed, You can startup php-json-server
with PHP build-in server.
# Startup with PHP build-in server
$ cd path/to/install
$ composer run --timeout 0 serve
Then visit the site at http://localhost:8080/. If the welcome page is displayed, installation and startup are successful.
Save data file in the path below.
path/to/install/data/db/schema_name.json
The schema_name
of file name is used to data name. The data name can be any name, and multiple datas can be saved under different name. There is a sample.json
in the initial state.
[
{
"id": 1,
"name": "Red",
"code": "#ff0000"
},
{
"id": 2,
"name": "Green",
"code": "#00ff00"
},
{
"id": 3,
"name": "Blue",
"code": "#0000ff"
}
]
Data must wrote by array of json format. Data structure is arbitrary, but id
column is required.
To get the data named schema_name
, access below URL with GET method.
$ curl -X GET 'http://localhost:8080/schema_name'
Default data rows per page is 20. if you want to change it, use rows
parameter.
$ curl -X GET 'http://localhost:8080/schema_name?rows=10'
How to change page is use page
parameter.
$ curl -X GET 'http://localhost:8080/schema_name?page=2'
You can search by using the column name as the parameter.
$ curl -X GET 'http://localhost:8080/schema_name?name=Green'
Normally, When multiple parameters are specified, the search will be an AND condition.
If you want to search with OR condition, use or
to search_type
parameter.
$ curl -X GET 'http://localhost:8080/sample?name[]=Red&name[]=Green&search_type=or'
The response header contains information about paging.
name | description |
---|---|
PJS-Total: | Total number of datas. |
PJS-pages: | Total number of pages. |
PJS-Rows: | Rows of current page. |
Example below receive the data that id
column value is 123.
$ curl -X GET 'http://localhost:8080/schema_name/123'
Use POST method for insert data.
$ curl -X POST 'http://localhost:8080/schema_name' \
-H 'Content-Type: application/json' \
--data-raw '{"name":"Gray","code":"#808080"}'
Created data will be returned.
{"id":4,"name":"Gray","code":"#808080"}
Use PUT method for update data.
$ curl -X PUT 'http://localhost:8080/schema_name/2' \
-H 'Content-Type: application/json' \
--data-raw '{"name":"Yellow","code":"#FFFF00"}'
Updated data will be returned.
{"id":2,"name":"Yellow","code":"#FFFF00"}
Use DELETE method for delete data.
$ curl -X DELETE 'http://localhost:8080/schema_name/4'
Deleted data will be returned.
{"id":4,"name":"Gray","code":"#808080"}