-2

I'm trying to return a json that has been ut8_encoded. I'm setting before it, the header to HTTP response code also.

   /**
     * Set the response header and HTTP response code and response the data
     * @param $data array of response to be encoded as json
     * @param int $status the http status (200 is default)
     * @return string the json presentation of the array given
     */
    public function response($data, $status = 200)
    {
        header("HTTP/1.1 " . $status . " " . $this->getRequestStatus($status));

        echo utf8_encode(json_encode($data, JSON_UNESCAPED_SLASHES));
    }

The results are coming back ok but with a warning -

Warning: Cannot modify header information - headers already sent by (output started at RestApi.php:115) in RestApi.php on line 113

edit: Sorry, I probably should of add it - The warning is talking about those two lines that causing the warning.

How can I remove the warning?

The error can occur if I'm adding this part to the .htaccess ?

<IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "*"
    Header set Content-Type "application/json; charset=utf-8"
</IfModule>

I just realized. So maybe I don't really need to encode the response

Aviv Paz
  • 1,051
  • 3
  • 13
  • 28

1 Answers1

2

Make sure there is no whitespace at the top of the file, or at the top/bottom of any included files. Furthermore, make sure you haven't echoed to the screen already.

Rob Foley
  • 601
  • 4
  • 6