Кракозябры в выходном .txt файле парсера

Есть такой парсер карточки товара интернет-магазина https://ketmin.ru/catalog/futbolki_s_printom/~futbolka-detskaya-ketmin-funny-cvbelyj-3250754514

$ch = curl_init();

$header  = array
(
  'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
  'Accept-Encoding: gzip, deflate',
  'Content-type: application/x-www-form-urlencoded'
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://ketmin.ru/catalog/futbolki_s_printom/~futbolka-detskaya-ketmin-funny-cvbelyj-3250754514');

$html = curl_exec($ch);
curl_close($ch);

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$description = $xpath->query("//div[contains(@class, 'description')]/div[@class='title--min'][not(contains(text(), 'Таблица'))]/..");
$descriptionArr = iterator_to_array($description);
$descriptionTerm = null;
$Description = null;

foreach ($descriptionArr as $descriptions) { 
    $descriptionTerm[] = $descriptions->nodeValue;
}

foreach ($descriptionTerm as $descriptions) {
    $Description .= trim($descriptions);
}

$arrMainParams = array(
    "description" => $Description
);

$jsonDataProduct = json_encode($arrMainParams, JSON_UNESCAPED_UNICODE);
file_put_contents("data_product.txt", $jsonDataProduct);

И вот, в выходной текстовый файл data_product.txt вместо русских символов попадают кракозябры вида Характери . В чем может быть причина? Кодировка на сайте UTF-8.


Ответы (1 шт):

Автор решения: Алексей Шиманский

Напишите либо:

@$dom->loadHTML('<?xml encoding="UTF-8">' . $html);

либо

$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");   
@$dom->loadHTML($html);
→ Ссылка