Skip to content

Instantly share code, notes, and snippets.

@rahulpandey
Created May 24, 2015 06:19
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 rahulpandey/28065b550ff6f8954ccb to your computer and use it in GitHub Desktop.
Save rahulpandey/28065b550ff6f8954ccb to your computer and use it in GitHub Desktop.
simple class which helpful writing webservices
<?php
class ResponseHandler {
private static $_instance;
private $_api_response_code;
private $_api_response;
// create new instance of class
public static function singleInstance() {
if (! self::$_instance) {
self::$_instance = new self ();
}
return self::$_instance;
}
private function __construct() {
$this->_api_response_code = array (
0 => array (
'HTTP Response' => 400,
'Message' => 'Unknown Error'
),
1 => array (
'HTTP Response' => 200,
'Message' => 'Success'
),
2 => array (
'HTTP Response' => 403,
'Message' => 'HTTPS Required'
),
3 => array (
'HTTP Response' => 401,
'Message' => 'Authentication Required'
),
4 => array (
'HTTP Response' => 401,
'Message' => 'Authentication Failed'
),
5 => array (
'HTTP Response' => 404,
'Message' => 'Invalid Request'
),
6 => array (
'HTTP Response' => 400,
'Message' => 'Invalid Response Format'
)
);
}
/**
*
* @param string $reponseFormate The desired HTTP response content type: [json, html, xml]
* @param int $responseCode The desires reponse for api
* @param $data $api_response The desired HTTP response data
*/
public function deliver($reponseFormate, $responseCode, $data) {
$response ['code'] = $responseCode;
$response ['status'] = $this->_api_response_code [$response ['code']] ['HTTP Response'];
$response ['status_message'] = $this->_api_response_code [$response ['code']] ['Message'];
$response ['data'] = $data;
// Return Response to browser. This will exit the script.
$this->deliver_response ( $reponseFormate, $response );
}
/*
* @param string $format The desired HTTP response content type: [json, html, xml]
* @param string $api_response The desired HTTP response data
* @return void
*/
function deliver_response($format, $api_response) {
// Define HTTP responses
$http_response_code = array (
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found'
);
// Set HTTP Response
@header ( 'HTTP/1.1 ' . $api_response ['status'] . ' ' . $http_response_code [$api_response ['status']] );
// Process different content types
if (strcasecmp ( $format, 'json' ) == 0) {
// Set HTTP Response Content Type
header ( 'Content-Type: application/json; charset=utf-8' );
// Format data into a JSON response
$json_response = json_encode ( $api_response );
// Deliver formatted data
echo $json_response;
} elseif (strcasecmp ( $format, 'xml' ) == 0) {
// Set HTTP Response Content Type
header ( 'Content-Type: application/xml; charset=utf-8' );
// Format data into an XML response (This is only good at handling string data, not arrays)
$xml_response = $this->toXml($api_response,'response');
// Deliver formatted data
echo $xml_response;
} else {
// Set HTTP Response Content Type (This is only good at handling string data, not arrays)
@header ( 'Content-Type: text/html; charset=utf-8' );
// Deliver formatted data
echo empty ( $api_response ['data'] ) ? $api_response ['status_message'] : $api_response ['data'];
}
// End script process
exit ();
}
/*
* The main function for converting to an XML document. Pass in a multi dimensional array and this recrusively
loops through and builds up an XML document.
@param array $data @param string $rootNodeName - what you want the root node to be - defaultsto data.
@param SimpleXMLElement $xml - should only be used recursively @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml = null) {
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get ( 'zend.ze1_compatibility_mode' ) == 1) {
ini_set ( 'zend.ze1_compatibility_mode', 0 );
}
if ($xml == null) {
$xml = simplexml_load_string ( "<?xml version='1.0' encoding='utf-8'?><$rootNodeName />" );
}
// loop through the data passed in.
foreach ( $data as $key => $value ) {
// no numeric keys in our xml please!
if (is_numeric ( $key )) {
// make string key...
$key = "unknownNode_" . ( string ) $key;
}
// replace anything not alpha numeric
$key = preg_replace ( '/[^a-z]/i', '', $key );
// if there is another array found recrusively call this function
if (is_array ( $value )) {
$node = $xml->addChild ( $key );
// recrusive call.
ResponseHandler::toXml ( $value, $rootNodeName, $node );
} else {
// add single node.
$value = htmlentities ( $value );
$xml->addChild ( $key, $value );
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML ();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment