hjaffer2001
hjaffer2001

Reputation: 953

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

Upvotes: 0

Views: 3399

Answers (2)

hjaffer2001
hjaffer2001

Reputation: 953

This is my edited PHP code.

<?php
        header("Content-type: text/html; charset=utf-8");
        $jsonurl = "http://api.tamilmagazine.com/";
        $json = file_get_contents($jsonurl,0,null,null);
        $json_output = json_decode($json);
        print_r($json_output);
        ?>

It worked. I forgot to mention the Content type.

Upvotes: 0

Robbie
Robbie

Reputation: 17720

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:

  1. 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)

  2. 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>';
  1. 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.

  2. You could also specify the encoding header using header() - but the php.ini directive may clash, so use one or the other.

  3. 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.

Upvotes: 2

Related Questions