Question: PHP JSON GET showing correctly on webpage, but not in php

Question

PHP JSON GET showing correctly on webpage, but not in php

Answers 1
Added at 2017-11-30 21:11
Tags
Question

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?

Answers to

PHP JSON GET showing correctly on webpage, but not in php

nr: #1 dodano: 2017-11-30 22:11

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.

Source Show
◀ Wstecz