I
I
Ilkhomjon Matazimov2020-04-15 09:30:45
PHP
Ilkhomjon Matazimov, 2020-04-15 09:30:45

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;


Here's what's in .htaccess:
#AddDefaultCharset UTF-8
AddDefaultCharset WINDOWS-1251


Here's what happens:
5e96aa0417998088369655.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilhomjon Matazimov, 2020-04-15
@mr_qpdb

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;

A
AUser0, 2020-04-15
@AUser0

Well add to the code

curl_setopt(CURLOPT_HTTPHEADER, array("Accept-Charset: WINDOWS-1251"));
suddenly help...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question