htmlspecialchars_decode无效

时间:2016-01-01 16:53:30

标签: json htmlspecialchars

我使用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>&lt;?php\n     include_once(\"class/db_inc.php\");\n?&gt;\n\n\n&lt;script type=\"text/javascript\"&gt;\n                    $('#movie').change(function() {\n                    var id=$(this).val();\n                    $.ajax\n                    ({\n                    type: \"POST\",\n                    url: \"ajax.php\",\n                    data: \"&amp;id=\"+id+\"&amp;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: \"&amp;id=\"+id+\"&amp;get_time=1\",   \n                    success: function(html)\n                    {\n                    $(\"#time\").append(html);\n                    } \n                    });\n                    });\n                &lt;/script&gt;\n    &lt;select class=\"list_of_movies\" name=\"movie\" id=\"movie\"&gt;&lt;option value=\"\"&gt;Select Movie&lt;/option&gt;\n                  &lt;?php\n                     $res=$schedule-&gt;getmoviedetail();\n                        while ($row=$connect-&gt;fetchArray($res))\n                        {\n                          $movie_id=$row['movie_id'];\n                          $title=$row['movie_title'];\n                          echo \"&lt;option value='$movie_id'&gt;$title&lt;/option&gt;\";\n                        }\n                    ?&gt;\n    &lt;/select&gt;\n\n                        &lt;select class=\"list_of_movies\" name=\"date\" id=\"date\"&gt;&lt;option value=\"\"&gt;Select Date&lt;/option&gt;\n                        &lt;/select&gt;\n\n                        &lt;select class=\"list_of_movies\" name=\"time\" id=\"time\" &gt;&lt;option value=\"\"&gt;Select Time&lt;/option&gt;&lt;/select&gt; \n                       &lt;div class=\"tab_desc\"&gt;\n                       &lt;a href=\"movie-select-show.html\"&gt;Book Now&lt;/a&gt;\n                       &lt;/div&gt;\n</code></pre>\n\n<p>And my ajax.php goes like this</p>\n\n<pre><code>&lt;?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.= \"&lt;option value='\".$sid.\"'&gt;\".$sdate.\"&lt;/option&gt;\";\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.= \"&lt;option value='\".$sid.\"'&gt;\".$stime.\"&lt;/option&gt;\";\n    }\necho $time;\n}\n?&gt; \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_decodehtml_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();
}    

0 个答案:

没有答案