public static function sendResponse($status = 200, $body = '', $content_type = 'text/html', $file_err = '') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // set the status header($status_header); // set the content type header('Content-type: ' . $content_type); // pages with body are easy if ($body != '') { // send the body echo $body; exit; } else { // create some body messages switch ($status) { case 401: $message = 'You must be authorized.'; break; case 404: $message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.'; break; case 500: $message = 'The server encountered an error processing your request.'; break; case 501: $message = 'The requested method is not implemented.'; break; case 'Fail': $message = $file_err; break; case 'auth_error': $message = 'Try another login.'; break; } // servers don't always have a signature turned on (this is an apache directive "ServerSignature On") $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; // this mybe templatized ... $body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <p>' . $message . '</p> <hr /> <address>' . $signature . '</address> </body> </html>'; echo $body; exit; } }
public static function sendResponse($body = '', $status = 200, $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // set the status header($status_header); // set the content type header('Content-type: ' . $content_type); // pages with body are easy if ($body != '') { // send the body echo $body; exit; } else { // create some body messages $message = ''; // 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: $message = 'You must be authorized to view this page.'; break; case 404: $message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.'; break; case 500: $message = 'The server encountered an error processing your request.'; break; case 501: $message = 'The requested method is not implemented.'; break; } // servers don't always have a signature turned on (this is an apache directive "ServerSignature On") $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; // this should be templatized in a real-world solution $body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <p>' . $message . '</p> <hr /> <address>' . $signature . '</address> </body> </html>'; exit($body); echo $body; exit; } }
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // set the status header($status_header); // set the content type header('Content-type: ' . $content_type); // set the location header if the status is 201 if ($status == '201') header('Location: '.$_SERVER['REQUEST_URI'].'/'.$body); // pages with body are easy if($body != '') { // send the body header('Content-Length: '.strlen($body)); echo $body; exit; } // we need to create the body if none is passed else { // servers don't always have a signature turned on (this is an apache directive "ServerSignature On") $signature = $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT']; // this should be templatized in a real-world solution $body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <address>' . $signature . '</address> </body> </html>'; header('Content-Length: '.strlen($body)); echo $body; exit; } }
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); header($status_header); header('Content-type: ' . $content_type); if ($body != '') { echo $body; exit; } else { $message = ''; switch ($status) { case 401: $message = 'You must be authorized to view this page.'; break; case 404: $message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.'; break; case 500: $message = 'The server encountered an error processing your request.'; break; case 501: $message = 'The requested method is not implemented.'; break; } // servers don't always have a signature turned on (this is an apache directive "ServerSignature On") $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; $body = '<!doctype html> <html> <head> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <p>' . $message . '</p> <hr/> <address>' . $signature . '</address> </body> </html>'; echo $body; exit; } }
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // establecemos status header($status_header); // el tipo de contenido header('Content-type: ' . $content_type); // mostramos exclusivamente el cuerpo si está creado if ($body != '') { echo $body; exit; } else { // en caso de que el cuerpo del mensaje sea vacio, necesitaremos construirlo // creamos la variable $message = ''; // Esta parte es opcional pero quedara mas presentable a los usuarios switch ($status) { case 401: $message = 'Debes estar autorizado para ver esta pagina.'; break; case 404: $message = 'La URL solicitada ' . $_SERVER['REQUEST_URI'] . ' noexiste.'; break; case 500: $message = 'Se ha encontrado un error al procesar la peticion.'; break; case 501: $message = 'El metodo solicitado no esta implementado.'; break; } // A veces la firma del servidor no esta activa (es la directiva apache "ServerSignature On") $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . 'Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; // Lo suyo es que esta parte este en una plantilla $body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title></head><body><h1>' . RestUtils::getStatusCodeMessage($status) . '</h1><p>' . $message . '</p><hr /><address>' . $signature . '</address></body></html>'; echo $body; exit; } }
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; }
/** * Throw exception. * * @param integer $httpCode * @throws \Exception */ private function throwException($httpCode) { if (null === $httpCode) { throw new \Exception('Something went wrong. StausCode is null.'); } else { $errorResponseBody = json_decode($this->getResponseBody(), true); $errorMessage = RestUtils::getStatusCodeMessage($httpCode) . '. '; if (is_array($errorResponseBody)) { if (array_key_exists('error', $errorResponseBody)) { $errorMessage .= $errorResponseBody['error'] . '.'; } else { if (array_key_exists('errors', $errorResponseBody)) { $errorMessage .= is_array($errorResponseBody['errors']) ? implode('. ', array_map('ucfirst', $errorResponseBody['errors'])) : $errorResponseBody['errors']; $errorMessage .= '.'; } } } throw new \Exception($errorMessage, $httpCode); } }
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') { //Set status and content type $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); header($status_header); header('Content-type: ' . $content_type); //Body if ($body != '') { if (is_array($body)) { echo json_encode($body); } else { echo $body; } exit; } else { //Server Signature $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; //Body $body = '{"message": "' . RestUtils::getStatusCodeMessage($status) . '", "code":"' . $status . '", "signature":"' . $signature . '"}'; /* <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-UTF-8"> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <p>' . $message . '</p> <hr /> <address>' . $signature . '</address> </body> </html>'; */ echo $body; exit; } }
public static function sendResponse($status = 200, $body = '', $content_type = 'json', $apiMode = true, $json_key = null) { $messages = RestUtils::getStatusCodeMessage($status); // Set the status $status_header = 'HTTP/1.1 ' . $status . ' ' . $messages['official']; header($status_header); // We need to create the body if none is passed (== error) if ($body === '' || $body === null) { $body = array("code" => $status, "message" => $messages['message']); } // And we can send the response if ($content_type == 'json') { /* ******************************* */ // JSON // /* ******************************* */ if ($apiMode == true) { // Now we add some metadata $response = array("provider" => "tuneefy", "api" => true, "version" => "0.9b", "status" => $status == 200 ? "splendid" : "bloody hell", "data" => $body); } else { $response = array("json_key" => $json_key, "data" => $body); } // Sets the content type header("Content-type: application/json; charset=UTF-8 "); echo json_encode($response); } else { if ($content_type == 'xml') { /* ******************************* */ // XML // /* ******************************* */ // Sets the content type header("Content-type: text/xml; charset=UTF-8 "); $xml = new XMLHelper(); // Adding the metadata $xml->push('response', array("provider" => "tuneefy", "api" => true, "version" => "0.9b", "status" => $status == 200 ? "splendid" : "bloody hell")); $xml->push('data'); function recursive_push($arrResult, $xml) { while (list($key, $value) = each($arrResult)) { if (is_array($value)) { if (is_numeric($key)) { // for elements that are integer keys, like js arrays $xml->push("item", array("rank" => $key)); } else { $xml->push($key); } recursive_push($value, $xml); $xml->pop(); } else { for ($i = 0; $i < count($value); $i++) { if (is_numeric($key)) { $xml->element("item", $value, array("id" => $key)); } else { $xml->element($key, $value); } } } } } recursive_push($body, $xml); $xml->pop(); // data $xml->pop(); // response echo utf8_encode($xml->getXml()); } } exit; }
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') { $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status); // set the status header($status_header); // set the content type header('Content-type: ' . $content_type); // pages with body are easy if ($body != '') { // send the body echo $body; exit; } else { // create some body messages $message = ''; // 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 400: $message = 'Please review the API format.'; break; case 401: $message = 'I\'m sorry, but you exceeded the demo query limits to validate NLPTools. I would be very glad to know about your interest in NLPTools and to work out a solution with you. If there is anything I can do for you, please don\'t hesitate to drop me a line at alex (at) atrilla.net --Alex'; break; } // servers don't always have a signature turned on (this is an apache directive "ServerSignature On") $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE']; // this should be templatized in a real-world solution $body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title> </head> <body> <h1>' . RestUtils::getStatusCodeMessage($status) . '</h1> <p>' . $message . '</p> <hr /> <address>' . $signature . '</address> </body> </html>'; echo $body; exit; } }
function sendFile($status = 200, $filename, $filesize, $body) { header('HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status)); header('Content-type: application/octet-stream'); header('Content-Disposition: filename="' . $filename . '"'); header('Content-length: ' . $filesize); header('Cache-Control: no-cache, must-revalidate'); header('Access-Control-Allow-Origin: http://www.sipcapture.org/'); header('Access-Control-Max-Age: 3600'); header('Server: Homer REST/1.0'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); if (ob_get_length()) { ob_clean(); } echo $body; exit; }