PHP Classes
elePHPant
Icontem

PHP JSON Response Class: Generate a HTTP request response in JSON format

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-09-18 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 160 This week: 89All time: 8,639 This week: 2Up
Version License PHP version Categories
json-response 1.0.0MIT/X Consortium ...5HTTP, PHP 5
Description Author

This class can generate a HTTP request response in JSON format.

It can take an a variable value as parameter and generates a JSON encoded string from that value to serve it as response for the current request.

The class can also take the string for the request status value and serves the respective HTTP response header being 200 for ok, 401 for unauthorized and 500 for exception.

Innovation Award
PHP Programming Innovation award nominee
September 2019
Nominee
Vote
HTTP requests need to have a response that uses status codes to let browsers or HTTP clients know if a request was handled successfully or some other outcome has happened.

HTTP status codes are numbers, which are usually not human friendly because they do not mean anything unless the developer looks up or remembers their meaning of all the HTTP status codes that you use in an application.

This class provides a more human friendly solution to issue HTTP status codes that consists in passing just the text message of the HTTP status, so the class automatically determines the HTTP status code numbers.

Manuel Lemos
  Performance   Level  
Name: Ogbemudia Osayawe <contact>
Classes: 3 packages by
Country: Malaysia Malaysia
Age: 36
All time rank: 377615 in Malaysia Malaysia
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 2x

 

Details

json-response

A simple class that returns a properly formatted json response with HTTP status

Installation

composer require devscreencast/json-response

Usage

<?php
require_once __DIR__ ."/vendor/autoload.php";

use Devscreencast\ResponseClass\JsonResponse;

$student = array(
    'name' => 'John Doe',
    'course' => 'Software Engineering',
    'level' => '200',
    'collections' => ['books' => 'Intro to UML', 'music' => 'rap']
);

new JsonResponse('ok', '', $student);

Param 1 (Required)

  1. success or ok - 200 http status
  2. unauthorized - 401 http status
  3. exception - 500 http status (internal server error)

Param 2 (Optional)

string - the return message, use empty quote if not available

Param 3 (Optional)

Array - Array of Data

  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

 Version Control Unique User Downloads Download Rankings  
 100%
Total:160
This week:89
All time:8,639
This week:2Up