我使用Stack Exchange api接收json数据。这个json数据有html权限,我需要转换回Character。 例如
我有像这样的JSON
{"items":[{"tags":["php"],"owner":{"reputation":1,"user_id":5736417,"user_type":"registered","profile_image":"https://www.gravatar.com/avatar/4514d5c24e8ae9a7b97f462e75ad245a?s=128&d=identicon&r=PG&f=1","display_name":"bishal.bh","link":"http://stackoverflow.com/users/5736417/bishal-bh"},"is_answered":false,"view_count":36,"answer_count":2,"score":0,"last_activity_date":1451677239,"creation_date":1451670332,"last_edit_date":1451676301,"question_id":34558481,"link":"http://stackoverflow.com/questions/34558481/creating-dynamic-dropdown-buttons","title":"Creating dynamic dropdown buttons","body":"<p>I have two tables in my database</p>\n\n<p>movie-> movie_id, movie_title</p>\n\n<p>screening-> scr_id, movie_id, scr_date, scr_start</p>\n\n<p>I am trying to make three dependent dropdown lists like this where date and time are automatically shown in the list according to the movie chosen.\n<a href=\"http://i.stack.imgur.com/ELynZ.png\" rel=\"nofollow\">Dropdown list.</a> \nI have called a javascript function above my select list like this</p>\n\n<pre><code><?php\n include_once(\"class/db_inc.php\");\n?>\n\n\n<script type=\"text/javascript\">\n $('#movie').change(function() {\n var id=$(this).val();\n $.ajax\n ({\n type: \"POST\",\n url: \"ajax.php\",\n data: \"&id=\"+id+\"&get_date=1\", \n success: function(html)\n {\n $(\"#date\").append(html);\n } \n });\n });\n\n\n $('#timeime').change(function() {\n var id=$(this).val();\n $.ajax\n ({\n type: \"POST\",\n url: \"ajax.php\",\n data: \"&id=\"+id+\"&get_time=1\", \n success: function(html)\n {\n $(\"#time\").append(html);\n } \n });\n });\n </script>\n <select class=\"list_of_movies\" name=\"movie\" id=\"movie\"><option value=\"\">Select Movie</option>\n <?php\n $res=$schedule->getmoviedetail();\n while ($row=$connect->fetchArray($res))\n {\n $movie_id=$row['movie_id'];\n $title=$row['movie_title'];\n echo \"<option value='$movie_id'>$title</option>\";\n }\n ?>\n </select>\n\n <select class=\"list_of_movies\" name=\"date\" id=\"date\"><option value=\"\">Select Date</option>\n </select>\n\n <select class=\"list_of_movies\" name=\"time\" id=\"time\" ><option value=\"\">Select Time</option></select> \n <div class=\"tab_desc\">\n <a href=\"movie-select-show.html\">Book Now</a>\n </div>\n</code></pre>\n\n<p>And my ajax.php goes like this</p>\n\n<pre><code><?php \ninclude_once(\"class/db_inc.php\");\n\nif($_REQUEST['get_date']){\n $sql=mysql_query(\"SELECT * FROM `screening` where `movie_id`=\".$_REQUEST['id']);\n $date=\"\";\n while($row=mysql_fetch_array($sql))\n {\n $sid=$row['scr_id'];\n $sdate=$row['scr_date'];\n\n $date.= \"<option value='\".$sid.\"'>\".$sdate.\"</option>\";\n }\necho $date;\n}\n\nelseif($_REQUEST['get_time']){\n$time=\"\";\n $sql=mysql_query(\"SELECT * FROM `screening` where `movie_id`=\".$_REQUEST['id']);\n\n while($row=mysql_fetch_array($sql))\n {\n $sid=$row['scr_id'];\n $stime=$row['scr_start'];\n\n $time.= \"<option value='\".$sid.\"'>\".$stime.\"</option>\";\n }\necho $time;\n}\n?> \n</code></pre>\n\n<p>The first dropdown shows the movie list fine. But the second and third dropdown are completely not working. I am stuck at this for a day now. Can anyone please help me with this? </p>\n"}],"has_more":false,"quota_max":300,"quota_remaining":293}
当我在htmlspecialchars_decode
或html_entity_decode
中传递此json时,输出仍然相同,它不会将这些实体转换为相关字符
这是代码。
header("Content-Type: application/json");
header("Content-Encoding: gzip");
if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])){
$id= $_REQUEST['id'];
$url = "https://api.stackexchange.com/2.2/questions/".$id."?site=stackoverflow&filter=withBody";
$data = file_get_contents($url);
//echo html_entity_decode($data,ENT_QUOTES);
echo htmlspecialchars_decode($data,ENT_QUOTES);
//echo $data;
exit();
}