Q:

JSON Parsing using PHP returns unicode/ascii character

I have a code lik this
            <?php

            $jsonurl = "http://api.tamilmagazine.com/";
            $json = file_get_contents($jsonurl,0,null,null);
            $json_output = json_decode($json);
            print_r($json_output);
            ?>

The above code returns a json response as.
        stdClass Object
        (
            [mag_id] => 1
            [mag_name] => à®†à®©à®¨à¯à®¤ à®µà®¿à®•à®Ÿà®©à¯
            [sid] => 544
            [bpath] => http://www.tamilmagazine.com/
            [api_path] => http://api.tamilmagazine.com/
            [categories] => Array
                (
                    [0] => stdClass Object
                        (
                            [cat_id] => 25
                            [cat_name] => à®…à®°à®šà®¿à®¯à®²à¯
                            [articles] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [aid] => 20053
                                            [a_title] => à®¤à®²à¯ˆà®¯à®™à¯à®•à®®à¯ - à®•à®±à¯à®±à¯à®•à¯ à®•à¯Šà®Ÿà¯à®™à¯à®•à®³à¯ à®®à¯‡à®¤à¯ˆà®•à®³à¯‡...
                                            [p_no] => 7
                                            [weburl] => tamilmagazine/msite.php?type=article&mid=1
                                        )

                            . . .. .. . . . . . . . . . . . . . . . . . . . and so on . . . .

When i view the given api (http://api.tamilmagazine.com/)  in browser i am getting the correct font lik this.
            {
                "mag_id": "190987",
                "mag_name": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ",
                "sid": "44",
                "bpath": "http://www.tamilmagazine.com/",
                "api_path": "http://api.tamilmagazine.com/",
                "categories": [
                    {
                        "cat_id": "25",
                        "cat_name": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ",
                        "articles": [
                            {
                                "aid": "3",
                                "a_title": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ...",
                                "p_no": "7",
                                "weburl": "msitee.php?type=article&mid=1"
                            },

            .  .. . . . .   and so on. . ... 

In other words, my api url works fine in browser, whereas, the api url after php parsing did not work for me in my browser, showing some special characters (i dont know whether its unicode or ascii).
Please advice. 
Thanks 
Haan                                         

A:

This is probably correct in the server - but viewing in a broswer it looks wrong. If you don't specify the character encoding of the output, or the setting in php.ini is wrong, then the browser will guess, and often gets it wrong.
A couple of ways to test:

Check the "source" by doign a view source. (Make sure you can view the source in a compatible text editor, though - otherwise the same might happen)
BEFORE you do the debug output, add in the correct HTML headers first to set the encoding:

echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';   // Set the charset appropriately! Looks like a cyrillic set?
print_r($json_output);
echo '</body></html>';

Next to check is your PHP output has the correct header. php.ini has a default setting for output: "default_charset". If this is blank, or wrong, set this to the apprpriate value either in php.ini or using ini_set(); and this wil tell php to specify a character encoding header.
You could also specify the encoding header using header() - but the php.ini directive may clash, so use one or the other.
Finally, if that fails, you need to decode manually. Check the comment in the manual (http://php.net/manual/en/function.json-decode.php) by "contacto at hardcode dot com dot 25-Nov-2010 01:53" as this has an example function

Other tricks you can use when handling the actual string would be to use
echo utf8_decode(print_r($json_output, true));

as this may convert to ISO more readily understood by the browser IF the original is UTF-8. Probably it's not utf-8 otherwise the browser should have understood and displayed it appropriately, but worth a shot if all else fails. 

