0
I am building a Rest API using PHP.
The data are accented correctly from the database and arrive in the application also correct. The problem is trying to access the API address from the browser. I want the data, when accessing through the browser, combine correct accentuation and indentation.
If I use:
echo json_encode(
$result, JSON_PRETTY_PRINT
);
the data are shown indented, but the accent is displayed wrong (Unicode).
If I use:
echo json_encode(
$result, JSON_UNESCAPED_UNICODE
);
the data is displayed with the correct accentuation, but the indentation does not exist (the data is all on a continuous line, making it difficult to read).
Remarks:
- The php file is formatted in utf-8 on save.
- The database is Mysql and is formatted also for utf-8.
- I tried to use
header('Content-Type: text/html; charset=utf-8');
andheader('Content-Type: application/json; charset=utf-8');
in PHP file without success. - The default charset of the server is UTF-8, according to phpinfo.
Thank you!
Which web server are you using? It may be that the server is not configured as UTF-8 correctly. The header('Content-Type: application/json; charset=utf-8' item is really required.
– epx
@Exp phpinfo shows that the default server charset is utf-8.
– Brunno Vianna
If you use as indicates doc does not answer? Eg. json_encode($input,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
– Marcos Xavier
I had a similar problem in TCC, even configuring everything with utf-8, when making a select, put some black diamonds in place of the special letters, what solved my problem was to give these query before selecting: mysqli_query($connected, "SET NAMES 'utf8'"); mysqli_query($connection, 'SET character_set_connection=utf8'); mysqli_query($connection, 'SET character_set_client=utf8'); mysqli_query($connection, 'SET character_set_results=utf8')');
– Leandro