Monday 30 October 2017

utf 8 - php Cannot modify header information

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 ?


            mod_headers.c>
Header set Access-Control-Allow-Origin "*"

Header set Access-Control-Allow-Methods "*"
Header set Content-Type
"application/json;
charset=utf-8"


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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...