My php web service uses json_encode to return json data fetched from MySQL database table. The data should be returned in whatever local language as unicode. Instead local characters like hindi/telugu/bengali are displayed as html entities. But output is needed as unicode and NOT html entities.
<?php
//header('Content-Type: text/html;charset: utf8'); //wasted code line
//Connection and credentials
$servername = "xxx.yyy.zzz.nnn";
$username = "username";
$password = "password";
$dbname = "mydb";
//variables to store fetched data
$item[]=array();
$dataArray[] = array();
// Create connection
$conn = mysql_connect($servername, $username, $password);
//mysql_set_charset('utf8', $conn); //wasted code line
$mytopid = mysql_real_escape_string($_GET['mytopid']); //get input
$query = "SELECT * FROM datamaster where Id > '$mytopid' order by Id desc"; //Now query
//mysql_query("set names 'utf8'"); //wasted code line
if ($result=mysql_query($query,$conn)){
$rows = mysql_numrows($result);
$i= 0;
while ($i < $rows){
//fetch data
$row = mysql_fetch_array($result);
//wasted code lines
//$dataArray[$i]["shortid"] = utf8_encode($row['Id']);
//$dataArray[$i]["shorttitle"] = utf8_encode($row['Title']);
//reality
$dataArray[$i]["shortid"] = $row['Id'];
$dataArray[$i]["shorttitle"] = $row['Title'];
$i++;
}
$item[0]["response"] = 1;
$item[1]["matching"] = $rows;
$item[2]["events"]=$dataArray;
echo json_encode($item);
}else{
$item[0]["response"] = 0;
}
//echo json_encode($item, JSON_HEX_TAG| JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); //wasted code line
mysql_close($conn);
?>
Got Output: Actual Output
Required Output: [{"shortid":"5","shorttitle":"\u0c38\u0c32\u0c4d\u0c2e\u0c3e\u0c28\u0c4d\u200c \u0c05\u0c21\u0c3f\u0c17\u0c3f\u0c28\u0c3e \u0c05\u0c35\u0c15\u0c3e\u0c36\u0c2e\u0c3f\u0c35\u0c4d\u0c35\u0c32\u0c47\u0c26\u0c41!"}]
I finally convert it at my client program decode to local language. The required output is supposed to be the default behaviour of json_encode. Despite most trials per php documentation (see the commented CODE lines that show my trials //wasted code line) the output continues to be in html entities except for English language.
My client programming language does not translate html entities.
Is there a php way to get the required output? I have tried every possible concept on stack overflow and in php documentation. Now I need help.