PHP Classes

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: 220 This week: 1All time: 8,270 This week: 560Up
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: 8 packages by
Country: Germany Germany
Age: 40
All time rank: 2943174 in Germany Germany
Week rank: 416 Up16 in Germany Germany Up
Innovation award
Innovation award
Nominee: 5x

Example

<?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('unauthorized', '', $student);


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:220
This week:1
All time:8,270
This week:560Up
User Comments (2)
Where is the class ?
4 years ago (José Filipe Lopes Santos)
55%StarStarStar
Where is the class ?
4 years ago (José Filipe Lopes Santos)
55%StarStarStar