Search code examples
phpheaderbufferbufferingoutput-buffering

Setting PHP headers within function / class method


I have written a class which outputs content type information as a header, however this does not work. After reading PHP.net it states

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called.

I am unsure what to do as I cannot hard code the content type everywhere needed. Does anyone know if it is possible to set headers in functions / classes at all?


Solution

  • The headers were sending correctly all along but I was checking the response of the file I was using and not properly getting the response from the CURL, if that makes sense! I just needed to use:

    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);