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 (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 198 All time: 8,327 This week: 259Up
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
Number 3
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
Picture of Ogbemudia Osayawe
  Performance   Level  
Name: Ogbemudia Osayawe <contact>
Classes: 4 packages by
Country: Germany Germany
Age: 37
All time rank: 3534198 in Germany Germany
Week rank: 240 Up12 in Germany Germany 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:198
This week:0
All time:8,327
This week:259Up
User Comments (2)
Where is the class ?
11 months ago (Josť Filipe Lopes Santos)
55%StarStarStar
Where is the class ?
11 months ago (Josť Filipe Lopes Santos)
55%StarStarStar
 

For more information send a message to info at phpclasses dot org.