Skip to content

Instantly share code, notes, and snippets.

@troyanvic
Created September 18, 2018 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyanvic/ada11618604a3dfb3d064f397292f101 to your computer and use it in GitHub Desktop.
Save troyanvic/ada11618604a3dfb3d064f397292f101 to your computer and use it in GitHub Desktop.
CodeIgniter JSON Response Controller - Converts data into JSON format and sends it back.
<?php
defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
/**
* CodeIgniter JSON Response Controller
* Converts data into JSON format and sends it back.
*
* @package CodeIgniter
* @author Troyan Victor, WebDesignSun Team
* @license MIT
* @version 1.0.0
*/
abstract class RESPONSE_Controller extends CI_Controller {
/**
* Set HTTP Status.
*
* @param int
*/
protected function set_HTTP_status( $code = 200 )
{
if ( function_exists( 'http_response_code' ) ) {
http_response_code( $code );
} else {
switch ( $code ) {
case 100:
$status = 'Continue';
break;
case 101:
$status = 'Switching Protocols';
break;
case 200:
$status = 'OK';
break;
case 201:
$status = 'Created';
break;
case 202:
$status = 'Accepted';
break;
case 203:
$status = 'Non-Authoritative Information';
break;
case 204:
$status = 'No Content';
break;
case 205:
$status = 'Reset Content';
break;
case 206:
$status = 'Partial Content';
break;
case 300:
$status = 'Multiple Choices';
break;
case 301:
$status = 'Moved Permanently';
break;
case 302:
$status = 'Moved Temporarily';
break;
case 303:
$status = 'See Other';
break;
case 304:
$status = 'Not Modified';
break;
case 305:
$status = 'Use Proxy';
break;
case 400:
$status = 'Bad Request';
break;
case 401:
$status = 'Unauthorized';
break;
case 402:
$status = 'Payment Required';
break;
case 403:
$status = 'Forbidden';
break;
case 404:
$status = 'Not Found';
break;
case 405:
$status = 'Method Not Allowed';
break;
case 406:
$status = 'Not Acceptable';
break;
case 407:
$status = 'Proxy Authentication Required';
break;
case 408:
$status = 'Request Time-out';
break;
case 409:
$status = 'Conflict';
break;
case 410:
$status = 'Gone';
break;
case 411:
$status = 'Length Required';
break;
case 412:
$status = 'Precondition Failed';
break;
case 413:
$status = 'Request Entity Too Large';
break;
case 414:
$status = 'Request-URI Too Large';
break;
case 415:
$status = 'Unsupported Media Type';
break;
case 500:
$status = 'Internal Server Error';
break;
case 501:
$status = 'Not Implemented';
break;
case 502:
$status = 'Bad Gateway';
break;
case 503:
$status = 'Service Unavailable';
break;
case 504:
$status = 'Gateway Time-out';
break;
case 505:
$status = 'HTTP Version not supported';
break;
default:
exit( 'Unknown http status code "' . htmlentities( $code ) . '"' );
break;
}
$protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header( $protocol . ' ' . $code . ' ' . $status );
}
}
/**
* Generate response.
*
* @param array
* @param NULL
*/
protected function send_JSON_response( $data, $code = NULL )
{
$this->send_response( NULL === $data ? NULL : json_encode( $data ), $code, array( 'Content-Type' => 'application/json' ) );
}
/**
* Send Data in response.
*
* @param array
* @param NULL
* @param array
*/
protected function send_response( $data, $code = NULL, $headers = array() )
{
if ( NULL === $data && NULL === $code ) {
$code = 404;
} else if ( NULL === $code ) {
$code = 200;
}
$this->set_HTTP_status( $code );
foreach ( $headers as $key => $value ) header( $key . ': ' . $value );
echo $data;
// exit
$this->terminate( 0 );
}
/**
* Complete the query.
*
* @param int
*/
protected function terminate( $code = 0 )
{
exit( $code );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment