4

my code:

<?php 
    header('Content-Type: application/json; charset=utf-8');
    $url = "http://80.211.192.133:8117/stats";
    $json = file_get_contents($url);
    $obj = json_decode($json);

    $error = json_last_error();

    var_dump($error);
?>

I am getting error:

code 5 - Malformed UTF-8 characters when encoding callback response 

but when you open the link from $url variable, it is showing correct data.

can someone help me with that?

8
  • what does echo $json show? Commented Nov 30, 2017 at 21:04
  • 1
    @KamrulKhan mR�n�0�cϪAI��: �CS�ࢇ��\YD(� ������I�'iV3���+Hg[}�� g�c����MN���m��`���10Hh�7hզ^��#ܒWbA����;�V/� �ǭ��従�����'�N�΋���Un��$B�8���-QѠ��YzH \��3?~�$��BTΊ�Qf��o��r����V�� ��s^�v���ۜ${m��ң���c�Qk��&W��Y���s�h�|>��ɂB�����+���#�b��1vKh�S�+�(�����Ul:0-,�(�� ��2�}w��!g ��l��s@z����֟Ļ��G��ʒ̓Y�����l�7�Y��i��ڭIxU9Sz�џɧHI��9���4G�󌕋?�3&J�J�J�:�j֢� �e�*�ڪHU^򩯐�q̎5O��+�3��_ Commented Nov 30, 2017 at 21:09
  • 1
    I sincerely doubt this is a charset issue. I believe it is more likely due to the fact that the source is delivering the content using Content-Encoding: deflate. It might be a duplicate of something here, but certainly not the one currently indicated. Commented Nov 30, 2017 at 21:22
  • i agree, this is not a duplicate at all. i tried playing with the deflate in PHP, but never done that much. sorry, no "help" time left, but does not seem like a charset issue at all. Commented Nov 30, 2017 at 21:23
  • 1
    @Machavity this is NOT a duplicate question. Commented Nov 30, 2017 at 21:28

1 Answer 1

4

The issue comes from the server compressing your data. gzinflate will help you out here:

<?php 
header('Content-Type: application/json; charset=utf-8');
$url = "http://80.211.192.133:8117/stats";
$data = file_get_contents($url); 
$data = gzinflate( $data ); 
$obj = json_decode($data,true);

In all fairness, @Octopus also spotted the important part.

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

1 Comment

It is unfortunate that PHP doesn't automatically do this when it sees the header. There is no guarantee that the source will always use the same Content-Encoding. To be safe your PHP should check for the presence of the header and its value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.