Переадрессация при парсинге сайта

Всем привет! Подскажите, пож, пытаюсь парсить страницу сайта

    function send_http($url, $ua)
{
    $ch = curl_init($url);
    $headers = [
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: ru-RU,en;q=0.5',
        "Host: " . parse_url($url) ["host"],
      'User-Agent: ' . $ua,
    ];
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    $html = curl_exec($ch);
    if (curl_errno($ch)) {
        print "Error: " . curl_error($ch);
        exit();
    }


    curl_close($ch);

    return $html;
}

$page = send_http('https://link/', get_ua());

Суть в том, что сайт автоматически перенаправляется на link/ua (украинскую версию сайта) и curl забирает именно эту украинскую версию, а мне нужно парсить link. Как передать команду парсингу, чтобы русскоязычную версию забирал?


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

Автор решения: void

Так у вас явно директива указана для curl

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

Эта директива обязывает следовать redirect-заголовкам. Замените на FALSE (хотя, можно и удалить - если мне не изменяет память, по умолчанию перенаправление игнорируется и так).

Пока писал ответ, выше в комментариях написали аналогичное.

P.S. Игнорировать / удалить cache: добавьте

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);

UPDATE: перенаправление вызывается для определённых UA. Попробуйте так - меня сработало:

function send_http($url, $ua)
{
    $ch = curl_init($url);
    $headers = [
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: en-US,en;q=0.5',
        "Host: " . parse_url($url)["host"],
        'User-Agent: ' . $ua,
        'Cache-Control: no-cache'
    ];
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    $html = curl_exec($ch);
    if (curl_errno($ch)) {
        print "Error: " . curl_error($ch);
        exit();
    }


    curl_close($ch);

    return $html;
}

$page = send_http('https://apeiron.school', "curl");
→ Ссылка