1

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

    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
  • Попробуйте curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    – TehnoMAN
    3 янв в 19:24

1 ответ 1

1

Так у вас явно директива указана для 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");
11
  • У меня же указан этот параметр
    – Mad Jack
    3 янв в 19:34
  • Укажите тогда явным образом: curl_setopt($curl1, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache"));
    – void
    3 янв в 19:35
  • C:\Users\qwerty>curl -L -I apeiron.school HTTP/1.1 200 OK Server: nginx Date: Tue, 03 Jan 2023 19:37:24 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate Pragma: no-cache Set-Cookie: PHPFRONTSESSID=d047ae7f797255a6a6fba00f8d99eaab; path=/; domain=.apeiron.school; HttpOnly x-ray: wn39340:0.020/wa39340:D=28418
    – void
    3 янв в 19:38
  • У меня все равно на /uk перекидывает ((
    – Mad Jack
    3 янв в 19:47
  • 1
    Бомба. Спасибо огромное
    – Mad Jack
    3 янв в 20:28

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.