public static function sendResponse($status = 200, $body = '', $type = 'json') { if ($body == '') { $body = array(); } elseif (is_string($body) && ($type == 'json' || $type == 'xml')) { $body = array('result' => $body); } $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // set the status header($status_header); // set the content type if ($type == 'json') { header('Content-type: application/json'); } else { if ($type == 'xml') { header('Content-type: application/xml'); } else { header('Content-type: text/html'); } } // pages with body are easy if ($body != '') { // send the body if ($type == 'json') { $body = RestUtils::data_encode($body, 'json'); } else { if ($type == 'xml') { $body = RestUtils::data_encode($body, 'xml'); } } } else { // create some body messages $body = ''; // this is purely optional, but makes the pages a little nicer to read // for your users. Since you won't likely send a lot of different status codes, // this also shouldn't be too ponderous to maintain switch ($status) { case 401: $body = 'You must be authorized to view this page.'; break; case 404: $body = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.'; break; case 500: $body = 'The server encountered an error processing your request.'; break; case 501: $body = 'The requested method is not implemented.'; break; //TODO., more status? default? /*default: RestUtils::error();*/ } } echo $body; exit; }