Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Request get acceptable header


I'm building a rest api with Phalcon in a micro module. I want to get the accept header of the request to return an XML format if the accept header is a application/xml or if the accept header is a application/json then I'll return a json format. I seen in the documentation this method $request->getAcceptableContent(); but I don't know how to get the string application/xml or application/json with this method.

Could you help me ?


edited Apr '16

Hey, perhaps you can try with:

// by default you return json
$content = 'json';
if (strpos($this->request->getHeader('Accept'), 'application/xml') !== false) {
    $content = 'xml';

You can dump $this->request->getHeaders() to see all headers for testing

Would like to hear someone else's opinion if faced this :)

I think this is the good way to do this. Since I asked the question I tried that too and it's ok.

Thanks Nikolay.