Я новичок в PHP & JSON, и на основе учебника я сделал простой веб-сервис, который возвращает содержимое таблицы базы данных mysql.
Вывод осуществляется в формате XML и JSON, а также в базе данных.набор символов UTF-8.Моя проблема в том, что некоторые поля содержат греческие символы и не отображаются правильно в формате вывода JSON (в XML все в порядке).Любая идея, что может быть не так?
Файл PHP выглядит следующим образом:
<?php
/* require the place_name_en as the parameter */
/* soak in the passed variable or set our own */
$number_of_places = isset($_GET['num']) ? intval($_GET['num']) : 1; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
/* connect to the db */
$link = mysql_connect('xxx','xxx','xxx') or die('Cannot connect to the DB');
mysql_select_db('foodbar',$link) or die('Cannot select the DB');
mysql_set_charset('utf8',$link);
mysql_query('set names utf8');
/* grab the posts from the db */
$query = 'SELECT * FROM test';
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($place = mysql_fetch_assoc($result)) {
$places[] = array('place'=>$place);
}
}
if($format == 'json') {
/* output in json format */
header('Content-type: application/json');
echo json_encode(array('places'=>$places));
}
else {
/* output in xml format */
header('Content-type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<places>';
foreach($places as $index => $place) {
if(is_array($place)) {
foreach($place as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
/*echo '<',$tag,'>',htmlentities($val,ENT_QUOTES,"utf-8"),'</',$tag,'>';*/
echo '<',$tag,'>',$val,'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</places>';
}
@mysql_close($link);
?>
Вы можете проверить вывод XML здесь .
Нокогда возвращается json формат , возникает проблема с греческими символами.они выглядят как:
\ u03b4 \ u03b4 \ u03c3 \ u03c3 \ u03b4 \ u03b4 \ u03c6
Есть идеи?Заранее спасибо!
PS Настройки БД: MySQL charset: UTF-8 Unicode (utf8) и сопоставление соединений MySQL: utf_8_unicode_ci