Giter VIP home page Giter VIP logo

php-json-server's Introduction

php-json-sever

Build Status Version

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.

Installation and Startup

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.

Prepare data

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.

Read data

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'

Response header

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.

Find row

Example below receive the data that id column value is 123.

$ curl -X GET 'http://localhost:8080/schema_name/123'

Insert row

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"}

Update row

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"}

Delete row

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"}

php-json-server's People

Contributors

sawarame avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

php-json-server's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.