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?

honk
  • 9,137
  • 11
  • 75
  • 83
Fero Hora
  • 67
  • 6
  • what does `echo $json` show? – Kamrul Khan Nov 30 '17 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��_ – Fero Hora Nov 30 '17 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. – Octopus Nov 30 '17 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. – blueweb Nov 30 '17 at 21:23
  • ok, actualy, i found it : – blueweb Nov 30 '17 at 21:25
  • @blueweb GREAT - That worked, thanks a lot! Problem solved :) – Fero Hora Nov 30 '17 at 21:27
  • 1
    @Machavity this is NOT a duplicate question. – blueweb Nov 30 '17 at 21:28
  • @blueweb, you are correct. vote to reopen it. – Octopus Nov 30 '17 at 21:30

1 Answers1

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.

honk
  • 9,137
  • 11
  • 75
  • 83
blueweb
  • 166
  • 6
  • 1
    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. – Octopus Nov 30 '17 at 21:50