Answer the question
In order to leave comments, you need to log in
How to set curl encoding?
I'm trying to get the DOM tree using curl, but I can't set the encoding.
Here is the code:
function curl_get($url, $referer = 'www.google.com') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36");
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = curl_get('https://www.potehechas.ru/club/fact_1.shtml');
// file_put_contents('1.txt', $html);
echo $html;
#AddDefaultCharset UTF-8
AddDefaultCharset WINDOWS-1251
Answer the question
In order to leave comments, you need to log in
Solved the issue with iconv.
iconv is a UNIX utility for converting text from one encoding to another. Also ported to Windows.
Here is the code:
function curl_get($url, $referer = 'www.google.com') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36");
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$body = iconv('windows-1251', 'UTF-8', $data);
return $body;
}
$html = curl_get('https://www.potehechas.ru/club/fact_1.shtml');
echo $html;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question