php - Slim Framework -> XML output with correct headers -



php - Slim Framework -> XML output with correct headers -

writing api handle xml , json. want response in format request uses.

example - api request header has: accept: application/xml

the issue response has content-type: application/json.

i want homecoming content-type: application/xml

this code:

public function setheaders() { $headertype = $this->app->request->headers->get('accept'); switch($headertype){ case "application/xml": $this->app->response->headers->set("content-type",'application/xml'); default: // default type application/json $this->app->response->headers->set("content-type",'application/json'); } } # 404 errors $app->notfound(function () utilize ($app) { $logmessage = sprintf("404 not found: uri: %s", $app->request->getpath()); $app->log->debug($logmessage); $error = new \smstester\errorvo(2,"request doesn\'t exist, check manual."); $app->parser->setheaders(); $app->halt(404,$app->parser->outputparse($error)); });

outputparse returns next string:

<xml> <error>true</error> <errortype>request doesn\'t exist, check manual.</errortype> <errormessage>2</errormessage> </xml>

the problem not using break out of switch after right case fires (assuming $headertype beingness set application/xml) default case ends running , reverts changes made first case.

public function setheaders() { $headertype = $this->app->request->headers->get('accept'); switch($headertype){ case "application/xml": $this->app->response->headers->set("content-type",'application/xml'); break; //break here prevents next case firing default: // default type application/json $this->app->response->headers->set("content-type",'application/json'); } }

php http-headers slim

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -