PHP JSON GET displays correctly on a webpage but not in php

my code is:

<?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 get an error:

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

but when you open a link from a variable $url, it shows the correct data.

can someone help me with this?

+4
source share
1 answer

The problem comes from a server compressing your data. gzinflatewill help you:

<?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 fairness, @Octopus also noticed an important part.

+4
source

Source: https://habr.com/ru/post/1690091/


All Articles