PHP使用html_get_contents获取JSON API返回乱码求助
file_get_contents() Hey Kevin, let's break down why you're seeing garbled content when using file_get_contents() to fetch that API data—browsers automatically handle a bunch of behind-the-scenes stuff that PHP's default functions don't, so here are the most common fixes to try:
1. Handle Gzip Compression (Most Likely Culprit)
Many APIs return gzip-compressed content to save bandwidth. Browsers automatically decompress this, but file_get_contents() doesn't do that by default. That compressed data looks like garbage when you try to read it directly.
Fix this by adding an Accept-Encoding header to your request, then decompress the response:
// Create a context with gzip acceptance $context = stream_context_create([ 'http' => [ 'header' => 'Accept-Encoding: gzip' ] ]); // Fetch the compressed response $compressedResponse = file_get_contents('http://turtlepool.space:8117/live_stats', false, $context); // Decompress it $decodedResponse = gzdecode($compressedResponse); // Now parse the JSON $data = json_decode($decodedResponse, true);
2. Mimic a Browser's Request Headers
Some servers adjust their response based on the User-Agent header. If your request looks like a generic PHP script, it might return unexpected content. Add a browser-like User-Agent to your context:
$context = stream_context_create([ 'http' => [ 'header' => [ 'Accept-Encoding: gzip', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' ] ] ]); $compressedResponse = file_get_contents('http://turtlepool.space:8117/live_stats', false, $context); $decodedResponse = gzdecode($compressedResponse); $data = json_decode($decodedResponse, true);
3. Fix Character Encoding Mismatches
If you still see garbled text after decompression, the response might be using a non-UTF-8 encoding. Detect and convert it to UTF-8 (the standard for JSON):
// After decompression $encoding = mb_detect_encoding($decodedResponse); if ($encoding !== 'UTF-8') { $decodedResponse = mb_convert_encoding($decodedResponse, 'UTF-8', $encoding); } $data = json_decode($decodedResponse, true);
4. Use cURL for More Control
If file_get_contents() is still giving you trouble, cURL is a more flexible tool that handles compression and headers automatically:
$ch = curl_init('http://turtlepool.space:8117/live_stats'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // Auto-handle gzip curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true);
The reason the API works in your browser is that browsers automatically handle gzip decompression, send proper headers, and manage encoding—all things file_get_contents() doesn't do out of the box. Start with the gzip fix, as that's the most common cause of this issue!
内容的提问来源于stack exchange,提问作者Kevin Cuperus

