Giter Site home page Giter Site logo

zend-expressive-api's Introduction

Zend Expressive API

This is a skeleton application for building web APIs using zend-expressive.

We used the zend-expressive programmatic approach configuration. You can check out the public/index.php file to see the usage of middleware piping.

Setup

You need to use composer to install the project. You can run the following command:

$ composer install

Status

The status of this project is work-in-progress, please consider this if you want to use it.

Usage

This skeleton application includes some APIs example:

  • RPC calls, using simple anonymous functions;
  • REST API, using a service manager.

RPC

You can have al look at RPC calls using the public/rpc.php file. For instance, you can use the internal PHP web server to test it:

$ php -S 0.0.0.0:8080 -t public public/rpc.php

You can test it using a HTTP client. The API calls that you can test are:

GET `/api/ping`
GET `/api/hello/{name}`

Where {name} is a parameter that you can use to pass a string.

For instance, using HTTPie, you can call the /api/ping URL using the following command:

$ http GET http://localhost:8080/api/ping

You will get something like this:

HTTP/1.1 200 OK
Connection: close
Content-Length: 18
Content-Type: application/json
Host: localhost:8080

{
    "ack": 1476900096
}

For the /api/hello/{name} you can use the following request:

$ http GET http://localhost:8080/api/hello/Enrico
HTTP/1.1 200 OK
Connection: close
Content-Length: 18
Content-Type: application/json
Host: localhost:8080

{
    "hello": "Enrico"
}

Of course, you can have RPC API also using the following REST architecture. We used anonymous functions for PRC to show how can be easy to create simple APIs on the fly, with very good response time.

REST

We provide a REST API using a User resource with a simple SQLite db with the following schema:

CREATE TABLE users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name VARCHAR(80),
  email VARCHAR(255) NOT NULL,
  password VARCHAR(60) NOT NULL
);

We published the following URLs:

  • GET /api/user[/{id:\d+}]
  • POST /api/user
  • PATCH /api/user/{id:\d+}
  • DELETE /api/user/{id:\d+}

In order to execute the REST API you need to use the public/index.php file. Using the internal web server of PHP you can use the following command:

$ php -S 0.0.0.0:8080 -t public public/rpc.php

Here we reported some example of usage using HTTPie client:

GET

Request:

$ http GET http://localhost:8080/api/user

Response:

HTTP/1.1 200 OK
Connection: close
Content-Length: 254
Content-Type: application/json
Host: localhost:8080

{
    " users": [
        {
            "email": "[email protected]",
            "id": "1",
            "name": "Foo",
            "password": "$2y$10$34w0udB8WTSKEzkaRzgmHev8Lx5EcK07Fs.SMZXnNc8w3yNPUXjNW"
        },
        {
            "email": "[email protected]",
            "id": "2",
            "name": "Bar",
            "password": "$2y$10$9wTSa.QrGxP9Q3zjLC74cebwA1ro5a7JOzvFHnSCApPDoutRfvGmW"
        }
    ]
}

POST

Request:

$ http POST http://localhost:8080/api/user name=Baz [email protected] password=test --json

Response:

HTTP/1.1 201 Created
Connection: close
Content-Length: 0
Content-type: text/html; charset=UTF-8
Host: localhost:8080
Location: /api/user/3

The user Baz has been created in the following location /api/user/3. Note that the password are stored using the bcrypt algorithm.

PATCH

Request:

$ http PATCH http://localhost:8080/api/user/3 name=Enrico --json

Response:

HTTP/1.1 200 OK
Connection: close
Content-Length: 129
Content-Type: application/json
Host: localhost:8080
X-Powered-By: PHP/7.0.4

{
    "user": {
        "email": "[email protected]",
        "id": "3",
        "name": "Enrico",
        "password": "$2y$10$tRG3Wan7jTJOOm2w8Jy3Au4ViJwol1q6ZToykM7qhDRPv174cIf6u"
    }
}

DELETE

Request:

$ http DELETE http://localhost:8080/api/user/3

Response:

HTTP/1.1 204 No Content
Connection: close
Content-Length: 0
Content-type: text/html; charset=UTF-8
Host: localhost:8080

zend-expressive-api's People

Contributors

ezimuel avatar

Watchers

James Cloos avatar M. Goneau - iWatchLife avatar

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.