We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Request get acceptable header

Hi,

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 ?

Thanks.



93.7k
Accepted
answer
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.