0

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

9
  • json_decode’s output strings are encoded in UTF-8. Did you specify your script’s output encoding as UTF-8 as well? Commented Jun 2, 2012 at 5:55
  • @Gumbo - Could you please explain me briefly with the above code. plz :( Commented Jun 2, 2012 at 5:57
  • Read this: Specifying the character encoding Commented Jun 2, 2012 at 6:02
  • Thanks Gumbo. I have edited my code, and it worked. Please check my php code above(edited).. It worked. Thanks a lot Commented Jun 2, 2012 at 6:05
  • Um, you already specify your output’s character encoding as UTF-8 but it still doesn’t get displayed correctly? Commented Jun 2, 2012 at 6:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, the characters in the string are not in ISO 8859-1. So utf8_decode won’t work here.
@Gumbo - I realised that probably the same time! Added clarification about handling that too in the answer. Thanks. (Yep - I hate character encoding as much as the next guy!)
@hjaffer2001 - Great, thanks for confirming. Handling charsets is a pain. Also remember that, if you save in a database, make sure the database is talking the same charset too. Everything needs to align (or be handled).
Ok Robbie. Thanks for your valuable comments/help. Thanks a lot. I am speechless. :D
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.