I am trying to decode HTML entities from the JSON data returned by the Stack Exchange API but it's not working.

I have used both html_entity_decode and htmlspecialchars_decode but it doesn't effect anything.

Here is the PHP code:

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);
    //echo htmlspecialchars_decode($data);
    //echo $data;
    echo "Question id not available";

1 Answer 1


Several things:

  1. The problem is not HTML entities, it is that the API always returns compressed data. Don't try to use file_get_contents() for that.
  2. Use cURL, but note that using cURL with HTTPS is not straightforward. See Using cURL in PHP to access HTTPS (SSL/TLS) protected sites for more information.
  3. Those headers do not set how data is fetched from the API, they control how your PHP page is returned to your browser.
  4. The given headers are somewhat invalid too, unless you mean for your PHP page to server JSON -- which the question code does not do.
  5. If you want the HTML source for the body, use htmlentities()Doc.

Putting it all together, your code would be something like this:

if (isset ($_REQUEST['id'])  &&  !empty ($_REQUEST['id']) ) {
    $id         = $_REQUEST['id'];
    $url        = "https://api.stackexchange.com/2.2/questions/".$id."?site=stackoverflow&filter=!iCArD3YeTq5x1sbe4cmVxS";
    $ch         = curl_init ($url);

    curl_setopt ($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);  // WARNING!  This can be unsafe!
    $cmprJson   = curl_exec ($ch);
    curl_close ($ch);

    $json       = $cmprJson;
    //$json       = gzdecode  ($cmprJson); // Extra step needed on some server configs
    $qstData    = json_decode ($json);
    $qstBody    = $qstData->items[0]->body;

    echo "<h1>The question body, page-ready, is:</h1>";
    echo '<div style="margin: 0 2em 2em 2em; border: 1px dashed gray;">' . $qstBody . '</div>';

    $bodyHtml   = htmlentities ($qstBody);
    echo "<h1>The question body HTML is:</h1>";
    echo '<div style="margin: 0 2em 2em 2em; border: 1px dashed gray;">' . $bodyHtml . '</div>';

    exit ();
    echo "Question id not available";
    exit ();
  • I am calling my php page with ajax and parsing json with javascript. using htmlentities() will not make sense because i want data to be decoded in simple plain text (Not the html source). I'll try the curl code.
    – Skyyy
    Commented Jan 2, 2016 at 22:09
  • Then just use $qstBody. The question is not clear on your ultimate/real purpose. Commented Jan 2, 2016 at 22:13
  • I just wanted to know why i am not able to decode html entities. I'll try this code and let you know.
    – Skyyy
    Commented Jan 2, 2016 at 22:16
  • Important: The gzdecode() step is only needed on some server configurations. See the updated answer. Commented Jan 3, 2016 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .