PHP CURL isn't processing encoded return data properly

PHP CURL isn't processing encoded return data properly

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Real Problem Behind "Ari Skúlason" in PHP CURL

You are fetching a JSON response that contains Ari Skúlason instead of Ari Skúlason. This specific string corruption—where an accented character appears as two or more strange characters—is almost always the result of a mismatch between the encoding declared in the HTTP headers and the actual bytes sent by the server, compounded by how PHP interprets those bytes.

Your current approach attempts to fix this after the fact using mb_convert_encoding. The logic you are applying is inverted. You are trying to convert UTF-8 into ISO-8859-1, but the data on your screen suggests the opposite might be happening, or the server is sending a double-encoded mess.

Let's look at the specific bytes involved. In ISO-8859-1 (often called Windows-1252 in web contexts), the character ú is represented by the byte sequence 0xC3 0xA9. In UTF-8, the same character is 0xC3 0xA9 as well. However, if you see ú, those are two separate characters: à (byte 0xC3) and ú (byte 0xA9). This specific pattern indicates that a UTF-8 string was interpreted as ISO-8859-1 during the fetch or initial decoding phase.

The critical missing piece in your curl implementation is the handling of the Content-Type header. When you execute the request, PHP's curl_exec downloads the raw body. If the server sends a header like Content-Type: application/json; charset=ISO-8859-1, but actually sends UTF-8 bytes, or vice versa, standard string functions will fail without explicit intervention.

The most robust solution is to ensure your client explicitly tells the server what it accepts and then decodes the response based on the actual bytes received, not an assumed encoding.

First, verify the Content-Type header returned by the server. If you inspect the raw response using a tool like curl -v, look for the charset parameter in the HTTP headers. Often, third-party APIs omit this parameter or set it incorrectly. In your specific case, the URL is hosted on cdn.content.easports.com. Many legacy CDNs default to ISO-8859-1 or even ASCII declarations for compatibility, while serving UTF-8 content underneath.

To handle this in PHP without relying on fragile manual conversions, you should let PHP's internal encoding detection take the lead or explicitly force UTF-8 if the server is known to serve it. However, mb_detect_encoding can be unreliable on JSON because it might misidentify the encoding of the string content itself versus the file encoding.

A more precise fix involves ensuring the response is treated as UTF-8 immediately upon receipt. If you are certain the source is UTF-8 (which is standard for modern JSON APIs), you should convert the incoming string to UTF-8 explicitly, rather than trying to convert it to ISO-8859-1 as your code snippet suggests.

Here is a refined implementation that addresses the encoding mismatch directly:

$location = 'http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0-B199-2E2A11D1CC13/2014/fut/items/web/179899.json';

$ch = curl_init($location);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Explicitly request UTF-8 to ensure the server sends it if it supports negotiation
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json; charset=utf-8',
    'Accept-Language: en-us' // Sometimes helps force proper encoding on legacy servers
));

$r = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($r === false) {
    // Handle network error
}

// If the data looks corrupted (double encoded), we need to force a decode.
// The string "ú" is UTF-8 bytes interpreted as Latin-1.
// To fix this, we convert FROM ISO-8859-1 TO UTF-8.
if ($r !== false && strlen($r) > 0) {
    $decoded = mb_convert_encoding($r, 'UTF-8', 'ISO-8859-1');
    
    // Verify the fix worked
    if (str_contains($decoded, 'Skúlason')) {
        echo "Success: " . $decoded;
        
        // Proceed with JSON parsing
        $data = json_decode($decoded);
    } else {
        echo "Encoding mismatch persists. Check server headers.";
    }
}

The key here is the direction of the conversion. If you see ú (double characters), the data was received as Latin-1 bytes but displayed in a UTF-8 environment. You must convert from ISO-8859-1 to UTF-8. Your original code attempted to do the reverse, which is why it produced nonsense like Ari Sk�lason.

If the server consistently returns ISO-8859-1 encoded JSON despite claiming otherwise, you must treat every response as Latin-1 first, decode it to UTF-8 in memory, and then parse the JSON. This ensures that names like Skúlason are handled correctly regardless of what the HTTP headers claim.

For high-volume applications where this specific CDN issue is problematic, consider using a library like guzzlehttp/guzzle which provides more granular control over encoding options, or simply cache the decoded result if the API endpoint remains static.

Our network