cURL gets response with utf-8 BOM
- by Reshat Belyalov
In my script I send data with cURL, and enabled CURLOPT_RETURNTRANSFER. The response is json encoded data. When I'm trying to json_decode, it returns null. Then I found that response contains utf-8 BOM symbols at the beginning of string ().
There is some experiments:
$data = $data = curl_exec($ch);
echo $data;
the result is
{"field_1":"text_1","field_2":"text_2","field_3":"text_3"}
$data = $data = curl_exec($ch);
echo mb_detect_encoding($data);
result - UTF-8
$data = $data = curl_exec($ch);
echo mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
// identical to echo mb_convert_encoding($data, 'UTF-8', 'UTF-8');
result - {"field_1":"text_1","field_2":"text_2","field_3":"text_3"}
The one thing that helps is removing first 3 symbols:
if (substr($data, 0, 3) == pack('CCC', 239, 187, 191)) {
$data = substr($data, 3);
}
But what if there will be another BOM? So the question is:
How to detect right encoding of cURL response? OR how to detect what BOM has arrrived? Or maybe how to convert the response with BOM?
Thanks.