https://github.com/harryosmar/php-restful-api-response
php restful api response implement PSR-7: HTTP message interfaces
https://github.com/harryosmar/php-restful-api-response
composer-plugin php psr7-http response-code rest-api transformer
Last synced: about 1 month ago
JSON representation
php restful api response implement PSR-7: HTTP message interfaces
- Host: GitHub
- URL: https://github.com/harryosmar/php-restful-api-response
- Owner: harryosmar
- Created: 2018-02-14T06:40:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-22T23:38:44.000Z (about 7 years ago)
- Last Synced: 2025-06-14T03:42:41.728Z (9 months ago)
- Topics: composer-plugin, php, psr7-http, response-code, rest-api, transformer
- Language: PHP
- Size: 38.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# php rest-api response implement PSR-7: HTTP message interfaces
[](https://github.com/harryosmar/php-restful-api-response/releases)
[](https://travis-ci.org/harryosmar/php-restful-api-response)
[](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/build-status/master)
[](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/?branch=master)
[](https://scrutinizer-ci.com/g/harryosmar/php-restful-api-response/?branch=master)
## Requirements
- php >= 7.0
- composer https://getcomposer.org/download/
## Features
- Implement PSR-7: HTTP message interfaces, extend https://github.com/zendframework/zend-diactoros
- Provides response format [collection](#with-collection) & [item](#with-item) using library http://fractal.thephpleague.com/
- Provides basic [errors response](#error)
## How To Setup
- *add this lines to your `composer.json` file*
```
{
"require": {
"harryosmar/php-restful-api-response": "^1.1"
}
}
```
- *then run `composer install` or `composer update`*
## How To Use
Simple example how to use
```
withArray([
'status' => 'created',
'id' => 1
], 200); //response code 200
```
response
```json
{
"status": "created",
"id": 1
}
```
## Available Response Format
* [with array](#with-array)
* [with item](#with-item)
* [with collection](#with-collection)
* [error](#error)
* [With Error](#with-error)
* [403 Forbidden](#403-forbidden)
* [500 Internal Server Error](#500-internal-server-error)
* [404 Not Found](#404-not-found)
* [401 Unauthorized](#401-unauthorized)
* [400 Bad Request](#400-bad-request)
* [410 Gone](#410-gone)
* [405 Method Not Allowed](#405-method-not-allowed)
* [431 Request Header Fields Too Large](#431-request-header-fields-too-large)
* [422 Unprocessable Entity](#422-unprocessable-entity)
##### With Array
```php
withArray([
'status' => 'created',
'id' => 1
], 201); //response code 201
```
response
```json
{
"status": "created",
"id": 1
}
```
##### With Item
For this sample, we use [class Book](https://github.com/harryosmar/php-restful-api-response/blob/master/tests/unit/Lib/Book.php) as an item
```php
withItem(
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
201
);
```
response 201
```json
{
"data":
{
"title": "how to be a ninja",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2017,
"price": 100000
}
}
```
##### With Collection
```php
withCollection(
[
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a mage', 500000, 2016),
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a samurai', 25000, 2000),
],
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
200
);
```
response 200
```json
{
"data": [
{
"title": "how to be a ninja",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2017,
"price": 100000
},
{
"title": "how to be a mage",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2016,
"price": 500000
},
{
"title": "how to be a samurai",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2000,
"price": 25000
}]
}
```
#### Error
##### With Error
```php
withError(['error' => 'something is wrong, please try again'], 500);
```
response 500
```json
{
"error": "something is wrong, please try again"
}
```
##### 403 Forbidden
```php
errorNotFound();
```
response 403
```json
{
"error":
{
"http_code": 403,
"phrase": "Forbidden"
}
}
```
##### 500 Internal Server Error
```php
errorInternalError();
```
response 500
```json
{
"error":
{
"http_code": 500,
"phrase": "Internal Server Error"
}
}
```
##### 404 Not Found
```php
errorNotFound();
```
response 404
```json
{
"error":
{
"http_code": 404,
"phrase": "Not Found"
}
}
```
##### 401 Unauthorized
```php
errorUnauthorized();
```
response 401
```json
{
"error":
{
"http_code": 401,
"phrase": "Unauthorized"
}
}
```
##### 400 Bad Request
```php
errorWrongArgs([
'username' => 'required',
'password' => 'required'
]);
```
response 400
```json
{
"error":
{
"http_code": 400,
"phrase": "Bad Request",
"message":
{
"username": "required",
"password": "required"
}
}
}
```
##### 410 Gone
```php
errorGone();
```
response 410
```json
{
"error":
{
"http_code": 410,
"phrase": "Gone"
}
}
```
##### 405 Method Not Allowed
```php
errorMethodNotAllowed();
```
response 405
```json
{
"error":
{
"http_code": 405,
"phrase": "Method Not Allowed"
}
}
```
##### 431 Request Header Fields Too Large
```php
errorUnwillingToProcess();
```
response 431
```json
{
"error":
{
"http_code": 431,
"phrase": "Request Header Fields Too Large"
}
}
```
##### 422 Unprocessable Entity
```php
errorUnprocessable();
```
response 422
```json
{
"error":
{
"http_code": 422,
"phrase": "Unprocessable Entity"
}
}
```
## How To Run The Test
```
composer test
```
## How To Contribute
- Fork this repo
- post an issue https://github.com/harryosmar/php-restful-api-response/issues
- create the PR(Pull Request) and wait for the review