Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my API returning NULL while using PHP's `json_decode(file_get_contents($link),true);`

I’m trying to pull crypto-mining data from the server’s API; however, when I run the .php file, it comes back with null:

HTML/PHP

$meow_base_data = json_decode(file_get_contents($meow_base_api), true);
var_dump($meow_base_data);

var_dump only says "NULL" – no other information.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FYI – I did check json_decode (file_get_contents) returns null and used the JSON Lint link – the JSON comes back as "Valid".

Here is the JSON header info from the API:

"version": "0.0.3",
    "statusCode": 200,
    "headers": {
    "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET",
    "Content-Type": "application/json"
}

I’ve also tried without the decode and doing:

file_get_contents($meow_base_api);
var_dump($http_response_header);

…which comes back with…

array(14) { [0]=> string(15) "HTTP/1.1 200 OK" 1=> string(20) "Server: nginx/1.21.6" 2=> string(35) "Date: Fri, 21 Oct 2022 20:01:08 GMT" [3]=> string(30) "Content-Type: application/json" [4]=> string(17) "Connection: close" [5]=> string(21) "x-powered-by: Express" [6]=> string(30) "access-control-allow-origin: *" [7]=> string(131) "access-control-allow-headers: Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods" [8]=> string(33) "access-control-allow-methods: GET" [9]=> string(21) "vary: Accept-Encoding" [10]=> string(22) "content-encoding: gzip" [11]=> string(25) "cache-control: max-age=20" [12]=> string(22) "apicache-store: memory" [13]=> string(23) "apicache-version: 1.6.3" }

I’ve tried the API link with and without the ‘s’ (HTTP vs HTTPS) – no difference either way.

FULL API in response to a comment:

{"version":"0.0.3","statusCode":200,"headers":{"Access-Control-Allow-Headers":"Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods","Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET","Content-Type":"application/json"},"body":{"primary":{"config":{"coin":"Meowcoin","symbol":"MEWC","algorithm":"kawpow","paymentInterval":10800,"minPayment":10,"recipientFee":0.01},"blocks":{"valid":1098,"invalid":0},"shares":{"valid":1297,"stale":0,"invalid":0},"hashrate":{"shared":4988807485.295822,"solo":0},"network":{"difficulty":5315.531682902409,"hashrate":363014296311.9665,"height":66171},"payments":{"last":1666380432093,"next":1666391232093,"total":3246666.94166398},"status":{"effort":18.788831312282458,"luck":{"luck1":310.69,"luck10":124.89,"luck100":103.61},"miners":25,"workers":35}},"auxiliary":{"config":{"coin":"","symbol":"","algorithm":"","paymentInterval":0,"minPayment":0,"recipientFee":0},"blocks":{"valid":0,"invalid":0},"shares":{"valid":0,"stale":0,"invalid":0},"hashrate":{"shared":0,"solo":0},"network":{"difficulty":0,"hashrate":0,"height":0},"payments":{"last":0,"next":0,"total":0},"status":{"effort":0,"luck":{"luck1":0,"luck10":0,"luck100":0},"miners":0,"workers":0}}}}

Here is what it shows when I var_dump without the decode:

file_get_char output

The script for this I’m using is:

$fgc = file_get_contents($meow_base_api);
var_dump($fgc);

>Solution :

When json_decode() returns NULL and you’re sure you’re getting a response, the first debugging step should be to look at what you’re passing into the function.

So if the result for $meow_base_data = json_decode(file_get_contents($meow_base_api), true); is NULL, see what you’re actually getting by running var_dump(file_get_contents($meow_base_api));.

In your case, the result of this appears to be binary data. The clue as to what sort of data this is, is in the response headers:

Content-Type: application/json but combined with content-encoding: gzip. So you’re getting a Gzip’ed JSON response. You need to un-gzip the response before you can pass it through json_decode().

Unfortunately, this is not as straightforward as it sounds. Although PHP does come with several functions to decompress Gzip-data (gzdecode(), gzinflate(), gzuncompress() and zlib_decode()), it is usually a case of trial-and-error before you find the one you need to use.

Sometimes, none of these functions are able to decompress the data out of the box. This can happen if the server uses a specific method of compression that browsers understand natively but PHP’s functions don’t. In this case, there will be a 10-byte prefix and an 8-byte suffix to the compressed data, so you can try the functions like this for example: $data = gzinflate(substr($raw_data, 10, -8));.

There is a relatively easy workaround for this problem though: use cURL instead of file_get_contents() (use the CURLOPT_RETURNTRANSFER option). cURL has native support for compressed responses as long as the content-encoding header is present, and will transparently decompress the response body without you having to do anything.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading